栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第四章:异常

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第四章:异常

4.3.1异常体系

1,Exception 异常 java中程序一旦遇到异常,程序无法继续向下执行

Throwable

/

Error Exception

2,Error: 错误,一般是由虚拟机生生成并脱出的,不需要程序猿控制,程序媛不关注

Exception : 异常

检查时异常|编译时异常 : 编译期间遇到的异常,如果不处理,程序语法运行

运行时异常 : 运行期间遇到的异常,通过增强程序健壮性的代码就可以解决 if...

3,常见的运行时异常:

1.空指针异常 NullPointerException

2.数组索引越界 ArrayIndexOutOfBoundsException

3.类型转换异常 ClassCastException

4.数学异常 ArithmeticException

5.格式转换异常 NumberFormatException

public class Class01_Exception {
    public static void main(String[] args) {
        //1.空指针异常 NullPointerException
        System.out.println("main----->开始了");
        String str = null;
        if (str!=null)
        System.out.println(str.length());
        System.out.println("main----->结束了");
        //2.数组索引越界 ArrayIndexOutOfBoundsException
        int[] arr = new int[5];
        int i = 6;
        if (i>=0 && i 
4.3.2异常处理方案* 

1,异常处理方案: * 1.抛出异常 throws 异常类型 2.异常捕获 try...catch try{ 有可能出现异常的代码; }catch(FileNotFoundException e){ 遇到异常后执行的代码; }catch(NullPointerException e){ 遇到异常执行的代码; }..... catch(Exception e){//-->接盘侠 //Exception e = new ClassCastException(); } finally{ 无论try中是否遇到异常,结束之前都会执行finally中的代码 }

}

2,

注意: ​ 无论是编译时异常还是运行时异常都可以通过2中解决方案处理 ​ 但是编译时异常只能通过2种解决方案处理 ​ 而运行时异常可以通过2种解决方案处理,也可以通过增强程序健壮性if ​ 一个try后面可以接1~n个catch ​ 如果try中的代码一旦遇到异常try中后面的代码不会执行,会直接判断catch,多个catch之间从上到下判断,如果找到对应的catch,执行{}中的代码 ​ 如果存在多个catch,大范围类型的捕获放在最后 ​ 一般在finally中会定义资源的关闭等代码

public class Class02_Exception {
    public static void main(String[] args) {
        System.out.println("main----------");
        try {
            System.out.println("try------>开始了");
            //运行时异常
            test();
            System.out.println(10/0);
            System.out.println("try------>结束了");
        } catch (FileNotFoundException e) {
            System.out.println("遇到文件未找到异常");//
        }catch (ArithmeticException e){
            System.out.println("分母为0......");
        }finally {
            System.out.println("世上没有不散的宴席");
        }
        System.out.println("------结束了------");
    }
    public static void test() throws FileNotFoundException {
        //编译时异常
        InputStream in = new FileInputStream("D://test.txt");
    }
}
​
4.3.3自定义异常

1,自定义异常 自定义的异常类型需要直接或者间接的继承自Exception 自定义的异常类型为运行时异常需要直接或者间接的继承自RuntimeException throw 制造异常

public class Class03_Exception {
    public static void main(String[] args) {
        //创建对象
        User num = new User();
        num.setId(123456);
        num.setName("戴总");
        //num.setAge(20);
        //AgeException作为运行时异常
       
        //AgeException作为编译时异常
            try {
                num.setAge(28);
            } catch (AgeException e) {
                try {
                    num.setAge(18);
                } catch (AgeException ageException) {
                    ageException.printStackTrace();
                }
            }
            System.out.println(num);
        }
}
//自定义异常
//class AgeException extends RuntimeException { //运行时异常
    class AgeException extends Exception{//编译时异常
    public AgeException(){
    }
    public AgeException(String message){
        super(message);
    }
}
class User{
    private int id;
    private String name;
    private int age;
​
    public User() {
    }
​
    public int getId() {
        return id;
    }
​
    public void setId(int id) {
        this.id = id;
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public int getAge() {
        return age;
    }
    //运行时异常
    
        //编译时异常
    public void setAge(int age)throws AgeException {
        if (age<18 || age>100){
            throw new AgeException(age+"不合法");
        }
        this.age=age;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/592429.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号