您的位置:首页 > 其它

自己写的demo---声明异常同时处理异常,或者继续抛出异常

2015-11-15 17:24 477 查看
package exception;

public class exception {
public static void main(String args[])
{

/***
* 不能对类型 exception 中的非静态方法 a(int)进行静态引用,想要引用的时候非
* 静态的属性是不能够在静态的方法里引用的,想要引用的话必须是用对象来引用
*/
//a(1);
//new exception().a(1);//未处理的异常类型 Exception
//new exception().a(1);//可以继续抛出声明,这样子就不用try catch了
try {
new exception().a(1);
} catch (Exception e) {
System.out.println("try\tcatch处理了异常");
}

}
//这种写法会报错
/*    public void a(int i) {
if(i==1){
throw  new Exception();//未处理的异常类型 Exception
}
}*/
//这种写法没错,但是把异常自己处理了。相当于没卵用
/*    public void a(int i) {//抛出异常同时自己处理
if(i==1){
try {
throw  new Exception();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}//未处理的异常类型 Exception
}
}*/
public void a(int i) throws Exception {//声明抛出异常
if(i==1){
throw  new Exception();//抛出异常
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: