异常处理 1.04

异常处理 1.04,第1张

异常处理 1.04 基本概念
  1. 是程序中的不正常事件,不包括语法错误和逻辑错误
  2. Error(错误):Java虚拟机不能解决严重问题
  3. Exception(异常):外在因素或编译错误一般性问题
  4. 异常分运行时异常(可以不处理)和编译时异常(必须处理)
异常体系图

五种常见运行时异常
public class Exception00 {
    public static void main(String[] args) {
        //no1
        Integer[] integers = new Integer[3];
        integers[4] = 4;
        //no2
        System.out.println(1 / 0);
        //no3
            A b =   new B();
            B b2 = (B)b;
            C c = (C)b;
        //no4
        String a = null;
        System.out.println(a.length());
        //no5
        String  name = "smith";
        int i = Integer.parseInt(name);
        System.out.println(i);

    }
}
class A{}
class B extends A{}
class C extends A{}
处理异常

throws

try - catch (ctrl + alt + j)

public class Throws_ {
    public static void main(String[] args) throws Exception{
        try {
            System.out.println(1 / 0);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.println("!!!");
        }
    }
}
        System.out.println("!!!");
    }
}

}

					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5697569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存