捕获和异常抛出
1.常见的异常(exception)和错误(error)类型

出现下图的错误主要是因为当有多个捕获异常时,通常要把最大的放在最后,小的放在前面,大小关系见上图。

点击查看代码
public class Demo02 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        //产生try catch的快捷键
        // ctrl + alt + t 或command + option + t
        try {//try监控区域
            System.out.println(a/b);
        } catch (Exception e) {//catch(想要捕获的异常类型)捕获异常
            System.out.println("程序出现异常,b不能为0");
        } catch(Throwable e){
        }
        finally {//处理善后工作,比如使用scanner之后要与一个关闭
            System.out.println("finally");
        }
    }
}
(1)throw关键字抛出异常在方法体中抛出

(2)throws关键字抛出异常紧跟方法名之后

                    
                
                
            
        
浙公网安备 33010602011771号