0
点赞
收藏
分享

微信扫一扫

spring boot启动停止重启脚本

佳简诚锄 2024-11-20 阅读 27

在这里插入图片描述


Java-异常处理机制-throws

一、finally的使用说明

1、finally的理解

2、什么样的代码我们一定要声明在finally中呢?

package trycatchfinally;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * package:trycatchfinally
 *
 * @Author jimmy-yan
 * @Create 2024/11/19 11:59
 */
public class FinallyTest {

    public static void main(String[] args) {
        FinallyTest f = new FinallyTest();
        f.test1();
    }

    public void test1() {
        try {
            String str = "123";
            str = "abc";
            int i = Integer.parseInt(str);
            System.out.println(i);

        } catch (NumberFormatException e) {
            e.printStackTrace();
            System.out.println(10 / 0);  //在catch中抛出异常
        } finally {
            System.out.println("程序执行结束");
        }
    }


    public void test2() {

        FileInputStream fis = null;
        try {
            File file = new File("D:\\hello.txt");
            fis = new FileInputStream(file);  //可能报FileFonudException

            int data = fis.read(); //可能报IOException
            while (data != -1) {
                System.out.println((char) data);
                data = fis.read(); //可能报IOException
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();  //可能报IOException
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

二、异步处理的方式二:throws

1、格式

在方法的声明处,使用throws异常类型1,异常类型2,…

2、举例

public void test() throws 异常类型1,异常类型2,..{
	//可能存在编译时异常
}

3、是否真正处理了异常?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * package:PACKAGE_NAME
 *
 * @Author jimmy-yan
 * @Create 2024/11/19 13:58
 */
public class ThrowsTest {
    public static void main(String[] args) {
        test3();
    }

    public static void test3() {
        try{
            test2();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void test2() throws FileNotFoundException, IOException {

        File file = new File("D:\\hello.txt");
        FileInputStream fis = new FileInputStream(file);  //可能报FileFonudException

        int data = fis.read(); //可能报IOException
        while (data != -1) {
            System.out.println((char) data);
            data = fis.read(); //可能报IOException
        }
        fis.close();  //可能报IOException
    }
}

4、方法重写的要求(只针对编译型异常)

子类重写的方法抛出的异常类型可以与父类被重写的方法抛出的异常类型相同,或者是父类被重写的方法抛出异常类型的子类。

5、开发中,如何选择异常处理的两种方式?

三、使用throw手动抛出异常对象

1、为什么需要手动抛出异常

在实际开发中,如果出现不满足具体场景的代码问题,我们就有必要手动抛出一个指定类型的异常对象。

2、如何理解 自动vs手动 抛出异常

过程1:“抛”
“自动抛”:程序在执行的过程当中,一旦出现异常,就会在出现异常的代码处,自动生成对应异常类的对象,并将此对象抛出。
“手动抛”:程序在执行的过程当中,不满足指定条件的情况下,我们主动的使用"throw + 异常类的对象"方式抛出异常对象。

过程2:“抓”
狭义上讲:try-catch的方式捕获异常,并处理。
广义上讲:把“抓"理解为"处理"。则此时对应着异常处理的两种方式:

  • try-catch-fina
  • throws

3、如何实现手动抛出异常

在方法内部,满足指定条件的情况下,使用“throw 异常类的对象”的方式抛出。

/**
 * package:PACKAGE_NAME
 *
 * @Author jimmy-yan
 * @Create 2024/11/19 14:51
 */
public class Throw {
    public static void main(String[] args) {
        Throw t = new Throw();
        t.regist(-9);
        System.out.println(t);
    }

    int id;

    public void regist(int id) {
        if (id > 0) {
            this.id = id;
        } else {
//            System.out.println("输入的非法id");
            //todo 手动抛出异常
            throw new RuntimeException("输入的id非法");
        }
    }

    @Override
    public String toString() {
        return "Throw{" +
                "id=" + id +
                '}';
    }
}

注意一点:throws后的代码不能被执行,编译不通过

举报

相关推荐

0 条评论