public static void main(String[] args) { int[] whee = new int[5]; try { Blah testBlah = new Blah(); testBlah.xxx().toString(); } catch (NullPointerException e) { System.out.println("NullPointerException message: " + e.getMessage()); e.printStackTrace(); System.out.println("------------------------"); System.out.println(); } try { int[] whee2 = new int[-2]; } catch (NegativeArraySizeException e) { System.out.println("NegativeArraySizeException message: " + e.getMessage()); e.printStackTrace(); System.out.println("------------------------"); System.out.println(); } try { whee[9] = 3; } catch (IndexOutOfBoundsException e) { System.out.println("IndexOutOfBoundsException message: " + e.getMessage()); e.printStackTrace(); System.out.println("------------------------"); System.out.println(); } try { divide(whee, 10); } catch (MyException e) { System.out.println("Custom message: " + e.getMessage()); } try { divide(whee, -2); } catch (MyException e) { System.out.println("Custom message: " + e.getMessage()); } }
// This method invokes b(), which is declared to throw // one type of exception. We handle that one exception. public static void a(int i) { try { b(i); } catch (MyException e) { // Point 1. // Here we handle MyException and // its subclass MyOtherException if (e instanceof MySubException) System.out.print("MySubException: "); else System.out.print("MyException: "); System.out.println(e.getMessage()); System.out.println("Handled at point 1"); } }
public List<String> getValidationErrors() { List<String> errors = new ArrayList<>(); if (getDate() == null || getDate().length() == 0) { errors.add("Date is required"); } if (getAction() == null) { errors.add("Button is required"); } if (errors.size() > 0) { return errors; } if (getAction() != "Update Price") { errors.add("Button is invalid"); } try { Date transitionDate = dateFormat(getDate()); // TODO: if date input is smaller than database last entry, return error. } catch (MyException e) { errors.add(e.getMessage()); } try { String error = checkNumberFormat(getPrice()); if (!error.equals("")) { errors.add(error); } } catch (MyException e) { errors.add(e.getMessage()); } return errors; }