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

In Java, is there a way to get (catch) all exceptions instead of catching the exception separately?

+69
java file exception exception-handling
Jul 02 '09 at 18:16
source share
6 answers

If you want, you can add throwing suggestions to your methods. Then you do not need to immediately check proven methods. This way you can catch exceptions later (perhaps at the same time as other exceptions ).

The code looks like this:

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

You can then deal with exceptions if you do not want to deal with them in this method.

To catch all the exceptions that a block of code can block, you can do: (This will also catch the exceptions that 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 it works is because Exception is the base class for all exceptions. Thus, any exception that can be thrown is an Exception (upper case "E").

If you want to handle your own exceptions, first just add a catch before the generic Exception.

 try{ }catch(MyOwnException me){ }catch(Exception e){ } 
+83
Jul 02 '09 at 18:20
source share

While I agree that it is not a good style to catch an unhandled exception, there are ways to handle exceptions that provide excellent logging and the ability to handle unexpected things. Since you are in exceptional condition, you are probably more interested in getting good information than during the response, so instance performance should not be very popular.

 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 log it, and let it bubble up the hierarchy. throw e; } } 

However, this does not take into account the fact that IO can also cause errors. Errors are no exception. Errors are under a different inheritance hierarchy than Exceptions, although both share the base Throwable class. Since IO can cause errors, you can go so far as to catch a 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 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 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; } } 
+75
Jul 02 '09 at 19:04
source share

Catch the Exception Base Exception

  try { //some code } catch (Exception e) { //catches exception and all subclasses } 
+9
Jul 02 '09 at 18:18
source share

It is bad practice to catch an Exception - it is too wide, and you can skip something like NullPointerException in your own code.

For most file operations, an IOException is the root exception. Better to catch it, instead.

+6
Jul 02 '09 at 18:36
source share

Yes there is.

 try { //Read/write file }catch(Exception ex) { //catches all exceptions extended from Exception (which is everything) } 
+4
Jul 02 '09 at 18:19
source share

You mean catch Exception of any type which is caused, and not just certain exceptions?

If yes:

 try { ...file IO... } catch(Exception e) { ...do stuff with e, such as check its type or log it... } 
+2
Jul 02 '09 at 18:18
source share



All Articles