Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As a followup to dkatzel's answer, let's assume you start to read from the file in the same try block and the file tells you which value in a array of options to use:</p> <pre><code>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() ); } </code></pre> <p>Now you have absolutely no idea what really went wrong except through the stack trace. You can't handle any fixable problems. You can't tell in code whether the file doesn't exist (<code>FileNotFoundException</code>), you don't have access to the file, (<code>IOException</code>), if the first line wasn't an Integer (<code>NumberFormatException</code>), or the number was bigger than the array length (<code>ArrayIndexOutOfBoundsException</code>). If you wanted to print a default value if you couldn't read the number, you could instead catch a <code>NumberFormatException</code> and print the value instead of having to quit the entire program.</p> <p>I'll admit this is a pretty contrived example, but it should give you an explanation of why catching <code>Exception</code> is bad. Marko also has a very good answer, stating that it usually is better to let the exception propagate up (especially with <code>RuntimeException</code>s) than to create a bunch of messy code trying to deal with every single problem that can happen.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload