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.
source share