Why use IOexception instead of Exception when fishing?

I cannot correctly formulate this correctly so that the search engine can get any meaningful results.

try{ BufferedReader reader = new BufferedReader( new FileReader("foo.bar") ); } catch(Exception e){ println( e.getMessage() ); } 

So, FileReader throws only a FileNotFoundException , which, as I understand it, is an IOException , which is an Exception. Can someone explain why I would catch a FileNotFoundException or IOException instead of just specifying a general "exception" and there was no need to import an exception (i.e. Import java.io.FileNotFoundException;) ). Is it strictly for reading?

I caught the exception using all three names and I cannot find the difference.

EDIT: --------------------

 private BufferedReader askUserForFile(String prompt){ BufferedReader rd = null; while(rd == null){ try{ String filename = readLine(prompt); rd = new BufferedReader( new FileReader(filename) ); } catch(Exception e){ println(e.getMessage()); } } return rd; } 
+6
source share
6 answers

Exception is the mother of all exceptions, including all subclasses of RuntimeException . When you specify to catch it, you will get much more fish in the net than you wanted, for example NullPointerException s, IllegalArgumentException , etc.

While catching a generic Exception is something that needs to be done at some point in your code, catching it at any lower level is almost certainly wrong and can damage the behavior of your application.

A more important skill in learning Java is not how to catch exceptions, but how not to catch them, instead letting them spread the call stack towards the exclusion barrier, one common place in the code where all the errors are (usually by registering, rolling back the transaction etc.).

+7
source

The difference is that inside the code of the try block there may be other problems that other types of Exception may throw, including subclasses of RuntimeException (which should not be declared).

If you just catch an Exception , you will also catch all these other errors that might hide another problem. Also, your code inside the catch block cannot accept the exception that occurred due to the IOException, because any exception will be detected.

+6
source

As an answer to the dkatzel question, let me assume that you are starting to read from a file in the same try block, and the file tells you what value to use in the parameter array:

 String toPrint = {"Hi", "World", "I'm", "A", "String", "Array"}; try{ BufferedReader reader = new BufferedReader( new FileReader("foo.bar") ); String line = reader.readLine(); System.out.println(toPrint[Integer.parseInt(line)]); } catch(Exception e){ println( e.getMessage() ); } 

Now you absolutely do not know what really went wrong, with the exception of the stack trace. You cannot handle any problems that arise. You cannot indicate in the code if there is a file ( FileNotFoundException ), you do not have access to the file ( IOException ) if the first line was not an integer ( NumberFormatException ), or the number was longer than the length of the array ( ArrayIndexOutOfBoundsException ). If you want to print the default value, if you could not read the number, you can instead NumberFormatException and print the value instead of leaving the entire program.

I admit that this is a pretty far-fetched example, but it should give you an explanation of why catching Exception bad. Marco also has a very good answer, stating that it is usually better to throw an exception (especially with RuntimeException s) than to create a bunch of dirty code trying to deal with every problem that might happen.

+3
source

What if you want to do another action with different exceptions? You can make a catch block to throw an IOException and, for example, show it a message box. Then create another catch block for FileNotFoundException and create a new file and try to open it again or even rebuild the exception. I hope I have correctly explained. Hooray!

+1
source

The reason is that whenever you program, you have to think about all the possibilities, and it is useful to do something for a specific error. Exception is a catch method for all traps and will handle all exceptions the same way. IOException will catch any IO exceptions, so it will handle the file not found and another IO exception (like EOFException ) is the same. FileNotFoundException will catch only files that have not found exceptions, so you can handle it, and not just log it.

Some errors will occur, and the ability to handle each individual case will launch your program. In this case, the file is not found, you can select another file so that the program does not crash and handle the situation.

0
source

FileNotFoundException is an IOException , which is an Exception

You're right. But if you catch Exception objects, you will catch any exception thrown by your code, not just the FileNotFound exception.

Say your code can throw more than one kind of exception:

 try { /* * Code, code and more code */ } catch(ExceptionType1 e) { System.err.println("Something went wrong! It is a type 1 exception"); } catch(ExceptionType2 e) { System.err.println("Something went wrong! It is a type 2 exception"); } catch(Exception e) { System.err.println("Something went wrong! It is not any of the known exception types"); } 

Compare the above opportunity with this:

 try { /* * Code, code and more code */ } catch(Exception e) { System.err.println("Something went wrong! Can be exception type 1, 2 or something else"); } 

As you can see, differentiating exception types will help you understand what went wrong in your code.

0
source

All Articles