Java 实例 - 重载方法异常处理
以下实例演示了重载方法的异常处理:
Main.java 文件
publicclassMain{doublemethod(inti)throwsException{returni/0;
}booleanmethod(booleanb){return !b;
}staticdoublemethod(intx, doubley)throwsException{returnx + y ;
}staticdoublemethod(doublex, doubley){returnx + y - 3;
}publicstaticvoidmain(String[]args){Mainmn = newMain();
try{System.out.println(method(10, 20.0));
System.out.println(method(10.0, 20));
System.out.println(method(10.0, 20.0));
System.out.println(mn.method(10));
}catch(Exceptionex){System.out.println("exception occoure: "+ ex);
}}}
以上代码运行输出结果为:
30.0 27.0 27.0 exception occoure: java.lang.ArithmeticException: / by zero