Reputation:
I am going to deploy our Web Application under Production server. Is including printStackTrace under catch block in production environment acceptable? (becuase the logs under catch block doesn't help to know the exact cause for the error) So please tell me if is it aceptable to have printStackTrace under catch block?
For instance, I purposefully put an invalid port number and printStackTrace() gives me this information.
printStackTrace() :
java.lang.IllegalArgumentException: port out of range:80800
at java.net.InetSocketAddress.<init>(InetSocketAddress.java:118)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:395)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:234)
at sun.net.www.http.HttpClient.New(HttpClient.java:307)
at sun.net.www.http.HttpClient.New(HttpClient.java:324)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1014)
at com.tata.util.XmlClient.execute(HttpXmlClient.java:83)
Log style :
And where as the log gives this (I am using Apache commons logging mechanism for logging)
Log.write("**EXCEPTION INSIDE execute " + e, Log.INFO);
06/Jan/2012 16:25:55 - main:http-8080-2 <> <60990020>**EXCEPTION INSIDE execute java.lang.IllegalArgumentException: port out of range:80800
So please tell me if is it acceptable to have printStackTrace under catch block?
Upvotes: 5
Views: 3407
Reputation: 10943
Yes, it is acceptable to print stack traces for exceptions in production environments. You can still throw the exception upwards for handling if you want.
Upvotes: 0
Reputation: 7579
Well, doing it that way is a first approach to logging and surely it will help on solving problems when they show up. But you can improve that using logging libraries like log4j or commons logger, setting your application to use them and output extra data (DB queries, result of operations, etc.), so you will retrieve some things alongside the stacktrace, perhaps with a better format.
Also, having the container/web server log with traces often raises doubts in clients, while having a structured application log keeps them a li'l bit more calm. A matter of perception.
Upvotes: 0
Reputation: 10918
Having a stacktrace in your life app is certainly bad.
Why don't you create a popup for your printStackTrace and display it if a certain debug flag is active (not sure how this is done in java).
Usually you let all exceptions bubble up to where you need to handle them. A conntection error might be an information you want to display in your app, so youhandle it in your front end (e.g. in a popup).
And Andrei Bodnarescu is right, fixing your logger does the trick as well.
Upvotes: 1
Reputation: 15219
The log will show the stacktrace as well, but you're not using it correctlly. Try:
log.error(Object message, Throwable t);
When you do Log.write("**EXCEPTION INSIDE execute " + e, Log.INFO);
the plus sign used as String concatenator will in fact call the toString()
method on the e object, and append that to the "**EXCEPTION INSIDE execute "
String, never getting a change to get to the exception object or it's stack trace.
http://commons.apache.org/logging/guide.html#Logging a Message
Upvotes: 5