A try block may contain code that throws different exception types. : try catch « Statements « SCJP






import java.io.File;
import java.io.IOException;

public class MainClass {
  public static void main(String[] argv) {
    File myFile = new File(".");
    String s = "not a number";

    try {
      System.out.println("About to call.");
      int x = Integer.parseInt(s);
      myFile.getCanonicalFile();
      System.out.println("Call succeeded.");
    }
    catch (NumberFormatException xceptn) {
      System.out.println("Bad text! " + xceptn.getMessage());
      xceptn.printStackTrace();
    }
    catch (IOException xceptn) {
      System.out.println("File stress! " + xceptn.getMessage());
      xceptn.printStackTrace();
    }

  }
}
About to call.
java.lang.NumberFormatException: For input string: "not a number"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	at java.lang.Integer.parseInt(Integer.java:447)
	at java.lang.Integer.parseInt(Integer.java:497)
	at MainClass.main(MainClass.java:11)
Bad text! For input string: "not a number"








5.9.try catch
5.9.1.Exceptions come in two flavors: checked and unchecked.
5.9.2.A handler for a supertype of the exception.
5.9.3.Multiple catch Clauses and Work with finally
5.9.4.A try block may contain code that throws different exception types.
5.9.5.Rethrow exception, no try...catch