Sunday, 17 April 2011

Can you catch more than one exceptions in a single catch block?

Yes. If the exception class specified in the catch clause has subclasses, any exception object that is a subclass of the specified Exception class will be caught by that single catch block.
E.g..
try {
// Some code here that can throw an IOException
}
catch (IOException e) {
e.printStackTrace();
}
The catch block above will catch IOException and all its subclasses e.g. FileNotFoundException etc.

No comments:

Post a Comment