Throws Exception
package com.minte9.basics.exceptions;
public class Throws_Exception {
public static void main(String[] args) {
try {
check("wrong");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static boolean check(String s) throws Exception {
if (s.equals("wrong")) {
throw new Exception("Wrong value exception!");
}
return true;
}
}
Multiple Exceptions
package com.minte9.basics.exceptions;
public class Multiple_Exceptions {
public static void main(String[] args)
throws NullPointerException {
try {
test(-10);
String val = args[2];
System.out.println(val);
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println(ex.getMessage());
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
System.out.println("Always executed!");
}
}
public static void test(int n)
throws Exception, ArrayIndexOutOfBoundsException {
if (n < 0) {
throw new Exception("Positive number required");
}
}
}