简体   繁体   中英

Handling all exceptions

Will the below code handle any type of exception?

try
{
    // some code
}
catch(Exception e)
{
    // some code part
}

Or should I do like this:

try
{
    // some code 
}
catch(type of Exception e)
{
    // some coding stuff
}

You can try below things :

1) To catch all Exception object and its sub-types :

try {

     // potential exception throwing code

    }
    catch(Exception e){
 }

2) To catch all Throwable and sub-types :

 try {

     // potential exception and error throwing code

    }
    catch(Throwable t){
 }

3) To catch any XYZ Exception and its sub-types:

 try {

     // potential XYZException throwing code

    }
    catch(XYZException xyzException){
 }

Please refer to the Oracle site : http://docs.oracle.com/javase/tutorial/essential/exceptions/ , for more information .

try { 
   //some code 
} catch(Exception e) { 
   //some code part 
}

It will handle all type of exception because its parent class of all type of exception but if you are getting Error like AWTError or VirtualMachine Error -> OutOfMemoryError or StackOverFlowError then it will not handle it.

To handle all type of exception and error do like this.

try {
    // some code 
}
catch(Throwable throwable) {
   // some coding stuff
}

Example 2:

try {
    // some code 
}
catch(type of Exception e) {
   // some coding stuff
}

This will not handle all type of exception. If you write here NullPointerException then it will handle only NullPointerException .

Remember: Define the catch in such a way that put the sub class to first then parent class otherwise code become Unreachable and you will get compile time error.

try {
    // some code 
}
catch(NullPointerException e) {
   // some coding stuff
}
catch(Exception e) {
  // some coding stuff
}

It depends on the type of Exception in your catch statement catch (Exception e) will catch any Exception as it is the parent class of all Exceptions. catch (FileNotFoundException e) will only catch FileNotFoundExceptions.

This allows you to have multiple catch statements:

try {
...            
}
catch (FileNotFoundException e) {
        //do something with FileNotFoundException
} 
catch (IOException e) {
        //do something with IOException
}
try { some code } catch(Exception e) { some code part }

will catch any exception of type Exception or any subclass of it. If you want to catch any exception, use

try { some code } catch(Throwable e) { some code part }

then you will get the Error s (class not found etc.) as well.

Try should be followed by a catch. FileNotFoundException is a sub Exception of IOException. So it must come first. If you have some file opened and an exception occurs in try block you need to close that in finally block. The finally block always executes when the try block exits.

try {

} 
catch (FileNotFoundException e) {
    //do something with FileNotFoundException
} 
catch (IOException e) {
    //do something with IOException
}
finally
{
     // do some clean up operation
} 

http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html

http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html

http://www.programcreek.com/2009/02/diagram-for-hierarchy-of-exception-classes/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM