java


What to do when catching e(rror) in Java


In Java, I see a lot of codes like below.
What I am wondering is, is it enough just to show error message?
I am new to Java. What I want to learn is how to handle error efficiently, and know best practices for error-handling. In general, what should I do in catch block?
Example 1 : printStackTrace()
} catch (SomeException e) {
e.printStackTrace();
}
Example 2 : getMessage
} catch (SomeException e) {
e.getMessage();
}
Example 3 : show custom error message
} catch (IOException E) {
System.out.println("Error occured. Please try again.");
}
In short, the answer is depends. Do what your business logic asking for.
If you want to log it, log it.
If you want to see the error in console, print it to there.
If you want to throw back to the method caller, go do it.
There are no of possibilities and needs depending on the situation.
I'd recommend you starting with Exception section in Java tutorial.
What you should do in the catch block depends on your application.
Generally, if you're just logging the error, you should include the stacktrace to make debugging easier.
logger.error("Error occurred.", e);
e.printStackTrace prints the message to the console, you can use this when the user sees the console...
e.getMessage() return a String, so you can handle as you wish, like:
try {
} catch (IOException E) {
System.out.println("Error occured. Please try again."+ e.getMessage());
}
or use the getMessage() when you have no console, but, for instance, a GUI, you can set:
textField.setText(e.getMessage());
You should log the error using a Logger so you can decide where to write the excetion (es. a file) and also do something else, depending what your code is doing.
For example if you are writing into a database you should rollback the data to the previous state.
I also suggest you not to suppresses the exception, but let the "user" know something went wrong. You can catch the exception and wrap it into another one with a message more user-friendly.
you can also redirect to another page like error page while getting any exception in the try catch block .
you can also describe the error on the html page .
so user redirect to the error page if there are exception in catch block.
e.printStackTrace();
the above could be good practice since it will trace the stack and show what and where the problem is

Related Links

apache jmeter Test Script Recorder
Java: Something is wrong with my split method
EmptyStackException at java
Missing play-{version}.jar error
Indoor routing using Graphhopper
difference in use cases between a normal pool and a keyed pool
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version [closed]
What is differene between secondary namenode and passive namenode
Abstract Quilt Square
Java Incompatible conditional operand types int and boolean
Painting JFrame Difficulty
How to add asyncTask on android
Java swing with synth checkboxes/radio button borders disappear after long uptime
ElasticSearch - empty data showing up
How to create a sort of abstract super test class in JUnit 4?
Differences in safe publishing between volatile,final and synchronized

Categories

HOME
google-chrome-extension
angular-material
objectgears
xmpp
google-project-tango
fancybox
gorm
tomcat6
etl
windows-phone-7
messages
hex-editors
fallback
dosgi
ef-migrations
google-cloud-speech
abi
realex-payments-api
microsoft-r
dbclient
javascriptcore
create-table
xmlreader
form-data
scaffold
automake
space-complexity
webix-treetable
uft-api
opshub
broadcastreceiver
heightmap
eclipse-gef
jsch
clean-architecture
ionicons
media-player
sybase-asa
sqldf
orthogonal
passport-google-oauth
gridpane
django-debug-toolbar
lttng
yaws
pagerank
netmq
execute
intel-fortran
google-places
wordml
fouc
coveralls
rgeo
ideamart
mono-embedding
sniffer
clipperlib
lustre
oxwall
knuth
phalanger
bitcoinj
mesa
jmapviewer
android-screen-support
distutils
simba
mt
funscript
semantic-diff
punbb
window-management
celltable
subviews
asp.net-mvc-areas
jquery-ui-droppable
thunderbird-lightning
multi-tier
eqatec
putchar
django-notification
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App