How can I catch all the exceptions that will be thrown through reading and writing a file? How can I catch all the exceptions that will be thrown through reading and writing a file? java java

How can I catch all the exceptions that will be thrown through reading and writing a file?


If you want, you can add throws clauses to your methods. Then you don't have to catch checked methods right away. That way, you can catch the exceptions later (perhaps at the same time as other exceptions).

The code looks like:

public void someMethode() throws SomeCheckedException {    //  code}

Then later you can deal with the exceptions if you don't wanna deal with them in that method.

To catch all exceptions some block of code may throw you can do: (This will also catch Exceptions you wrote yourself)

try {    // exceptional block of code ...    // ...} catch (Exception e){    // Deal with e as you please.    //e may be any type of exception at all.}

The reason that works is because Exception is the base class for all exceptions. Thus any exception that may get thrown is an Exception (Uppercase 'E').

If you want to handle your own exceptions first simply add a catch block before the generic Exception one.

try{    }catch(MyOwnException me){}catch(Exception e){}


While I agree it's not good style to catch a raw Exception, there are ways of handling exceptions which provide for superior logging, and the ability to handle the unexpected. Since you are in an exceptional state, you are probably more interested in getting good information than in response time, so instanceof performance shouldn't be a big hit.

try{    // IO code} catch (Exception e){    if(e instanceof IOException){        // handle this exception type    } else if (e instanceof AnotherExceptionType){        //handle this one    } else {        // We didn't expect this one. What could it be? Let's log it, and let it bubble up the hierarchy.        throw e;    }}

However, this doesn't take into consideration the fact that IO can also throw Errors. Errors are not Exceptions. Errors are a under a different inheritance hierarchy than Exceptions, though both share the base class Throwable. Since IO can throw Errors, you may want to go so far as to catch Throwable

try{    // IO code} catch (Throwable t){    if(t instanceof Exception){        if(t instanceof IOException){            // handle this exception type        } else if (t instanceof AnotherExceptionType){            //handle this one        } else {            // We didn't expect this Exception. What could it be? Let's log it, and let it bubble up the hierarchy.        }    } else if (t instanceof Error){        if(t instanceof IOError){            // handle this Error        } else if (t instanceof AnotherError){            //handle different Error        } else {            // We didn't expect this Error. What could it be? Let's log it, and let it bubble up the hierarchy.        }    } else {        // This should never be reached, unless you have subclassed Throwable for your own purposes.        throw t;    }}


Catch the base exception 'Exception'

   try {          //some code   } catch (Exception e) {        //catches exception and all subclasses    }