异常

来源:互联网 发布:甬航数据交互平台 编辑:程序博客网 时间:2024/06/04 17:44
package class1;
class Math {
public int div(int i ,int j) throws Exception {
System.out.println("====计算开始=====");
int temp = 0 ;
try {
temp = i / j ;
} catch (Exception e ) {
throw e ;
} finally {
System.out.println(" ======计算结束===="); 
}
return temp ;
}
}


public class ThrowDemo02 {
public static void main(String args[]) {
Math m = new Math() ;
try {
System.out.println("除法操作:" + m.div(10, 0));
} catch (Exception e) {
System.out.println("异常产生:" + e);
}
}
}
//throw抛出一个异常对象
//throws向上抛(Exception,Error)
//尽量使用try-catch