Java-异常处理机制-throws


Java-异常处理机制-throws

  • 一、finally的使用说明
  • 1、finally的理解
  • 2、什么样的代码我们一定要声明在finally中呢?
  • 二、异步处理的方式二:throws
  • 1、格式
  • 2、举例
  • 3、是否真正处理了异常?
  • 4、方法重写的要求(只针对编译型异常)
  • 5、开发中,如何选择异常处理的两种方式?
  • 三、使用throw手动抛出异常对象
  • 1、为什么需要手动抛出异常
  • 2、如何理解 自动vs手动 抛出异常
  • 3、如何实现手动抛出异常
  • 一、finally的使用说明

    1、finally的理解

    1、我们将一定要被执行的代码声明在finally结构中
    2、更深刻的理解:无论try中或者catch中是否存在仍未被处理的异常,无论try中或catch中是否存在return语句等,finally中声明的语句都一定要被执行。
    3、finally语句和catch语句是可选的,但是finally不能单独使用
    4、try-catch可以嵌套使用

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

    我们在开发中,有一些资源,比如:(输入流、输出流、数据库连接、socket连接等资源),在使用完以后,必须显式的进行关闭操作,否则,GC不会自动的回收这些资源。进而导致内存泄漏。
    为了保证这些资源在使用完之后,不管是否出现了未被处理的异常的情况下,这些资源能被关闭。我们必须将这些操作声明在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、是否真正处理了异常?

    从编译是否能通过的角度看:看成是给出了异常万一要是出现时候的解决方案。此方案就是,继续向上抛出(throws)。
    但是,此throws的方式,仅是将可能出现的异常抛给了此方法的调用者,此调用者仍然需要考虑如何处理相关异常。从这个角度来看,throws的方式不算真正意义上处理了异常。

    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、开发中,如何选择异常处理的两种方式?

    1、如果程序代码中,涉及到资源的调用(流、数据库连接、网络连接等),则必须考虑使用try-catch-finally来处理,保证不出现内存泄漏。
    2、如果父类被重写的方法没有throws异常类型,则子类重写的方法中如果出现异常,只能考虑使用try-catch-finally进行处理,不能throws。
    3、开发中,方法a中依次调用了方法b,c,d等方法,方法b,c,d之间是递进关系。此时,如果方法b,c,d中有异常,我们通常选择使用throws,而方法a中通常选择使用try-catch-finally。

    三、使用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后的代码不能被执行,编译不通过

    作者:敲代码敲到头发茂密

    物联沃分享整理
    物联沃-IOTWORD物联网 » Java-异常处理机制-throws

    发表回复