Sunday, 17 April 2011

Why is not considered as a good practice to write a single catchall handler to catch all the exceptions?

You can write a single catch block to handle all the exceptions thrown during the program execution as follows :
try {
// code that can throw exception of any possible type
}catch (Exception e) {
e.printStackTrace();
}
If you use the Superclass Exception in the catch block then you will not get the valuable information about each of the exception thrown during the execution, though you can find out the class of the exception occurred. Also it will reduce the readability of the code as the programmer will not understand what is the exact reason for putting the try-catch block.

No comments:

Post a Comment