views:

76

answers:

5

I have a code that throws a bunch of Exceptions but each of them only contains a printStackTrace() method as shown below

} catch (SecurityException e) {
    // TODO Auto-generated catch block
    System.err.println(e);
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Is this sufficient or do I need to include additional statements like System.err.println(e)? Usually, if an exception occurs I am able to trace the source with the above alone.

+1  A: 

It depends on the exceptions. Obviously, with printStackTrace() the exception will be printed for you to debug (or users to report to you). However there is no additional error handling.

Example: If an IOException is thrown, you might want to show the user a error message specifying the exact error cause, or you might want to do another attempt, transparent for the user. Or you might want to abort the whole program if the operation is critical for the success of the whole task... etc.

ChrisM
+6  A: 

If there is something you can do to solve the problem, do it in the catch, if there is nothing you can do, then it is better to use a logging framework to register the exception than to use e.printStackTrace(); or System.err.println(e);

I personally recommend: http://www.slf4j.org/, but if you have masochistic tendencies you can try the very bad (but official) Java Logging API: http://download.oracle.com/javase/1.4.2/docs/guide/util/logging/ .

One extra advantage of SLF4J is that it can redirect its logging to the awful Java Logging API (that way you can use an elegantly designed API and still comform to the awfully designed (de jure not de facto) "standard"

SLF4J is easy to use, to log an exception all you have to do is write logger.error("some accompanying message", exception);, another of its advantages is that you can, for example, configure it to send you an email each time your application crashes (by using logback as the underlying logging engine)

Luxspes
+1 for suggesting slf4j
Waldheinz
+1  A: 

If you want to trace the source e.printStackTrace() is enough. Usually I put e.printStackTrace(); at DEBUG level. Also I add meaningful error message at ERROR level for the users.

Upul
A: 

I think you might be missing a bit about the basics of exceptions and exception handling.

The golden rule of exceptions is that they should be exceptional.

This is why you might have seen or read that you should never catch the base Exception - there is simply no way that your code can handle every time of exception.

So as a general rule you should only catch exceptions if you can handle them in a specific way. For example, if you're reading a user's details from a file and that fails you might choose to return a new user. What you don't want to do is simply catch the exception and log it. This leads to an application that is robust but simply swallows errors which leads to an extremely bad user experience.

If your method can't handle an exception it should simply not catch it and defer the exception handling to a higher level. This usually means an error message will be displayed to the user (at the top level).

Jaco Pretorius
A: 

If you can afford to use a logging framework like log4j, you'll be able to call

}catch(Exception e){ log.error("Exception occurred:",e}

making the log framework to log your custom message "Exception occurred" followed by the stack trace in your errorlog file

savo.fra