异常处理

来源:互联网 发布:修改定位软件 编辑:程序博客网 时间:2024/06/07 18:37

异常是指程序运行过程中所发生的不正常事件,他会中断正在运行的程序,解决异常的方法抓抛:
try//执行可产生异常的代码
catch捕获异常
finally无论是否发生异常代码都执行
throws//声明法方异常
throw抛出异常
public void run(){
try{

System.out.println(“请输入三个数看是否构成三角形”);
Scanner sc=new Scanner(System.in);
int a = sc.nextInt();
int b= sc.nextInt();
int c = sc.nextInt();

        if(a+b>c&&a+c>b&&b+c>a){            System.out.println("可以构成三角型");        }else{            throw new IllegalArgumentException();        }

}
catch(Exception e){
}

}