[Java] try-catch-finally demo code
// 要先在一個方法throw new 出一個例外,主程式才可以try-catch-finally
// "throws IllegalArgumentException " 在此可以不用,因為只有當一個Exception只有是Throwable的subclass,
// 但不是RumtimeException or Error 的sublcass時才需用到throws
class multi {
public static int abc(int a, int b, int c) throws IllegalArgumentException {
if(a<=0 || b<=0 || c<=0)
throw new IllegalArgumentException("shit!");
else
return a*b*c;
}
public static void main(String[] args) {
try {
System.out.println(abc(2,-3,4));
}
catch(IllegalArgumentException e) {
System.out.println("The parameters to abc were 2,-3,4");
System.out.println(e);
}
catch(Throwable e) {
System.out.println(e);
}
finally {
System.out.println("Fuck your mother");
}
}
}