Java异常处理

Java异常处理

1.异常的处理:抓抛模型
过程一:抛

程序正常执行的过程中,一旦出现异常,就在异常代码处生成一个对应异常类的对象,并将此对象抛出。
一旦抛出对象以后,程序终止。
关于异常对象的产生:1.系统自动生成的异常对象;2.手动生成一个异常对象,并抛出(throw)

过程二:抓

即对异常的处理方式:

1.try-catch-finally

2.throws

2.try-catch-finally
try {
    // 可能会发生异常的语句
} catch(ExceptionType e) {
    // 处理异常语句
} finally {
    // 清理代码块
}
2.1说明

​ 1.finally是可选的
​ 2.通过try将异常代码包起来,一旦出现一行,就会生成一个对应的异常类,根据此对象的类型,去catch中匹配;
​ 3.一旦try中的异常匹配到某个catch,就进入catch中进行异常处理,然后跳出try-catch结构,继续执行;
​ 4.catch中的异常类型如果没有子父类关系,不存在顺序关系;
​ 5.catch中的异常有子父类关系,则子类声明在前;
​ 6.常用的异常对象处理方式:String getMessage()和printStackTrace();
​ 7.try结构中声明的变量,在出了try结构之后不能使用;
​ 8.try-catch-finally结构可以嵌套;

@Test
    public void test(){
        String str = "123";
        str = "abc";
        try {
            int num = Integer.parseInt(str);
        }catch (NullPointerException e){
            System.out.println("出现空指针异常");
            //System.out.println(e.getMessage());
            //e.printStackTrace();
        } catch (NumberFormatException e){
            //System.out.println("出现数值转换异常");
            //System.out.println(e.getMessage());
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("出现异常");//Exception 是父类

        }
    }

使用try-catch-finally处理编译时异常,使得程序在编译时不再报错,但是运行时可能报错,相当于把编译时异常,延迟到运行时报错

2.2finally的使用

​ 1.finally是可选的
​ 2.finally中的语句一定会执行,即使catch中又出现了异常,有return语句,以及没有异常等,finally也会执行
​ 3.像数据库连接,输入输出流,网络编程Socket等资源,JVM是不能自动回收的,我们需要手动的进行资源释放,因此需要声明在finally当中,无论是否有异常,都会释放资源。

@Test
    public void testMethod(){
        int num = method();
        System.out.println(num);//出现异常
                                // 2
    }

    public int method(){
        try {
            int[] arr = new int[10];
            System.out.println(arr[10]);
            return 1;
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            return 2;
        }finally {
            System.out.println("出现异常");
        }
    }

针对文件打开读取关闭操作:

@Test
    public void test2(){
        FileInputStream fis = null;
        try {
            File file = new File("hello.txt");
            fis = new FileInputStream(file);

            int data = fis.read();
            while(data != -1){
                System.out.print((char)data);
                data = fis.read();
            }

        } catch (IOException e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        }finally {
            try {
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

开发中,由于运行时异常比较常见,所以不针对运行时异常使用try-catch-finally

3.throws+异常类型

​ 1.写在方法的声明处,指明可能出现的异常,一旦发生异常,会在异常代码处生成一个异常类对象,此对象满足异常类型时,会被抛出,异常后面的代码就不再执行。
​ 2.throws 只是将异常抛给调用者,但并没有解决异常,异常还需要try-catch-finally处理。

@Test
    public static void main(String[] args){
        try{
            method2();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    //throws + 异常类型
    public static void method2() throws IOException{
        method1();
    }

    public static void method1() throws FileNotFoundException, IOException{
        File file = new File("hello.txt");
        FileInputStream fis = new FileInputStream(file);

        int data = fis.read();
        while(data != -1){
            System.out.print((char)data);
            data = fis.read();
        }
        fis.close();

    }
4.开发中如何选择两种异常处理方式

​ 如果父类中被重写的方法没有throws方法处理异常,则子类也不能使用throws,子类只能使用try-catch-finally处理。

5.自定义异常类

​ 1.继承于现有的异常结构,Exception和RuntimeException
​ 2.提供全局变量:serialVersionUID
​ 3.提供重载的构造器

//自定义异常类
public class MyException extends Exception {
    static final long serialVersionUID = -7034897193246939L;
    public MyException(){}
    public MyException(String msg){
        super(msg);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值