This discussion is archived
4 Replies Latest reply: Nov 3, 2006 6:41 AM by 807607 RSS

Why generic exception is not better than individual exceptions?

807607 Newbie
Currently Being Moderated
Hi Forum,

I am new to Java and currently I am learning how to handle exceptions in Java. I read some articles and they mentioned that generic exceptions are not good and each exception should be captured individually. For example
 catch (Exception e ) {
     System.out.println("Exception: " + e.getMessage());
is a bad way of capturing rather one should capture like
catch (FileNotFoundException fnf ) {
     System.out.println("Exception: " + fnf.getMessage());
}
catch (NullPointerException npe) {
     System.out.println("Exception: " + npe.getMessage());
}

catch (IOException ioe ) {
     System.out.println("Exception: " + ioe.getMessage());
}
if these are all the ones that can occur.

Thanks in advance.
Regards,
Ravi