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

TLS handshake failure with JDK 7 - RECV TLSv1 ALERT: fatal, handshake_failure
How to package dll's with Jar in Netbeans build
What is this error in Java with incompatible types?
Trying to add JLanguageTool to my java project using Ecplise
Why Java allows multiple inheritance in interfaces and deny to do so in classes? [duplicate]
java.lang.unsatisfiedlinkerror : no Jsmile in java.library.path
Writing into JSONArray
Running Java from ASP.NET MVC doesn't work
Writing an integration test for aspect functionality in Spring
Java polynomial division infinite loop
Java: Check to see if String.contains matches exact word
Restlet - how to expose a resource with both HTTP/HTTPS?
For loop drawing PGraphics in an array [Processing]
java not displaying jpanel in jframe after button press
JNI allocation corrupt heap
how to set Diffie Hellman key size to 2048 in OpenJDK

Categories

HOME
google-chrome-extension
testng
keycloak
relay
objectgears
framework7
sqlite-net-extensions
filtering
baqend
blueprintjs
autotools
cross-validation
medical
iggrid
remote-access
flask-wtforms
after-effects
finite-automata
serilog
jqwidget
google-pagespeed
textfield
graphicsmagick
format-specifiers
firebase-crash-reporting
xpages-ssjs
traffic
web-api-testing
nouislider
bluestacks
yadcf
xquery-3.0
pdb
language-concepts
atl
cloud-code
force-layout
host
appfabric
file-format
dartium
jna
elasticsearch-plugin
email-templates
pdf-reactor
mime
skeleton-css-boilerplate
dotcover
dropbox-php
nativeapplication
jxcore
lift-json
windows-iot-core-10
capacity
sqlbulkcopy
cudafy.net
word-vba-mac
visual-c++-2008
savon
adxstudio-portals
transmitfile
mathematica-frontend
impresspages
connect-by
lttng
suffix-tree
nessus
independentsoft
browser-link
r-tree
jms2
thredds
mutation-observers
deis
ionic
method-parameters
iiviewdeckcontroller
skos
nsbutton
internet-connection
marmalade
resty-gwt
oracle-warehouse-builder
message-driven-bean
mcts
html5-notifications
floating-point-precision
commoncrypto
dbconnection
type-equivalence
coderush
funscript
broken-links
first-responder
getmessage
regsvr32
window-management
qt-jambi
appender
krl
h.323
.nettiers
noscript
zune
data-acquisition

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