class ThrowException {
static void math_div(int x, int y){
try{
if ( y == 0)
throw new ArithmeticException(" 除數為 0");
System.out.println("x="+ x + ";y=" + y + ";x / y = " + x/y);
} catch(ArithmeticException e){
System.out.println("例外內容: "+ e.getMessage());
}
}
}
public class ExceptionSelfThrow {
public static void main(String[] args) {
ThrowException.math_div(10, 5);
ThrowException.math_div(10, 0);
}
}
執行結果

沒有留言:
張貼留言