Java 关于异常处理

class Test{
public int devide(int x,int y) throws Exception{     //此方法抛出了一个异常,则如果要调用这个函数就必须要处理这个异常
return x/y;
}
}
public class TestException {

public static void main(String[] args)   /*throws Exception*/{          //在mian方法头写了throws Exception,下面的语句调用devide时异常被main抛出,不用写try-catch了,但不建议这么写(不会进行处理)
//TODO Auto-generated method stub
try{
new Test().devide(3, 0);

}catch(Exception e){
System.out.println(e.getMessage());
}      //如果没有try-catch语句,就会错误
System.out.println("Programme is running");
}

}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpwfx.html