Catching Multiple Exception Types To Improve Type Handling : Exception Catch « JDK 7 « Java






Catching Multiple Exception Types To Improve Type Handling


import java.util.InputMismatchException;

public class Test {
  public static void main(String[] args) {
    
    try {
      System.out.print("Enter a number: ");
      int number = 100;
      if (number < 0) {
        throw new InvalidParameter();
      }
      if (number > 10) {
        throw new AssertionError("Number was too big", new Throwable(
            "Throwable assertion message"));
      }
    } catch (InputMismatchException | InvalidParameter e) {
      e.addSuppressed(new Throwable());
      System.out.println("Invalid input, try again");
    } catch (final Exception e) {
      System.out.println("Invalid input, try again");
    }
  }
}
class InvalidParameter extends java.lang.Exception {
  public InvalidParameter() {
    super("Invalid Parameter");
  }
}

 








Related examples in the same category

1.Catching multiple exception types to improve type checking