publicstaticvoidmain(String[] args) { System.out.println(divsion(10,0)); } /** * description: 测试除法异常 **/ publicstaticintdivsion(int a, int b )throws ArithmeticException{ return a / b ; } }
/** * 输出:
Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.divsion(Main.java:27) at Main.main(Main.java:17) **/
4.2 thorw关键字
使用示例
publicclassMain {
publicstaticvoidmain(String[] args) { System.out.println(divsion(10,0)); } /** * description: 测试除法异常 **/ publicstaticintdivsion(int a, int b )throws ArithmeticException{ if (b == 0){ thrownewArithmeticException("除数不能为0!"); } return a / b ; } }
/** * 输出:
Exception in thread "main" java.lang.ArithmeticException: 除数不能为0! at Main.divsion(Main.java:47) at Main.main(Main.java:39) **/