分享

Day10

 行者花雕 2022-11-11 发布于北京

异常

 

什么是异常?

  1. 实际工作中,遇到的情况可能不是非常完美的。比如:你写的某个模块,用户输入不一定符合你的要求、你的程序要打开某个文件,这个文件可能不存在或者文件格式不对,你要读取数据库的数据,数据库可能是空的,我们程序跑着,内存或硬盘可能满了等等

  2. 软件程序在运行的过程中,非常可能遇到刚刚提到的这些异常问题,我们叫异常。英文是:Exception,意思是例外。

  3. 异常指程序中余小宁出现不期而至的各种情况,如:文件找不到、网络连接失败、非法参数等

  4. 异常发生在运行期间,它影响了正常的程序执行流程

 1  package com.exception;
 2  3  public class demo01 {
 4      public static void main(String[] args) {
 5          new demo01().a();
 6  7      }
 8  9      public void a() {
10          b();
11      }
12 13      public void b() {
14          a();
15      }
16  }

Exception in thread "main" java.lang.StackOverflowError

 package com.exception;
 ​
 public class demo01 {
     public static void main(String[] args) {
         System.out.println(11 / 0);
     }
 }

Exception in thread "main" java.lang.ArithmeticException: / by zero

 

简单分类

检查性异常:最具代表性异常是用户错误或问题引起的异常,这是程序员无法预见的,例如要打开一个不存在的文件时,一个异常就发生了,这些异常在编译时不能被简单的忽略。

运行时异常:运行时异常是可能被程序员避免的异常。与检查性异常相反,运行时异常可以在编译时忽略。

错误ERROR:错误不是异常,而实脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译时也检查不到。

 

异常处理框架:

Java中把异常当作对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类.

Error和Exception的区别:Error通常是灾难性的致命错误,是程序无法控制和处理的,当这些异常出现时,Java虚拟机(JVM)一般会选择终止线程;Exception通常情况下可以被程序处理,并且在程序中应该尽可能的去处理这些异常。

 

1. 异常处理机制

抛出异常

捕获异常

异常处理五个关键字: try catch finally throw throws

 1  package com.exception;
 2  3  public class Test {
 4      public static void main(String[] args) {
 5          int a = 1;
 6          int b = 0;
 7          try {  //try 监控区域
 8              System.out.println(a / b);
 9          } catch (ArithmeticException e) {  //捕获异常
10              System.out.println("程序出现异常,变量b不能为0");
11          } finally {  //处理善后工作,资源的关闭
12              System.out.println("finally");
13          }
14 15      }
16  }

程序出现异常,变量b不能为0 finally

 1  package com.exception;
 2  3  public class Test {
 4      public static void main(String[] args) {
 5          int a = 1;
 6          int b = 0;
 7          try {  //try 监控区域
 8              new Test().c();
 9          } catch (Throwable e) {  //catch(想要捕获的异常类型) 捕获异常
10              System.out.println("程序出现异常");
11          } finally {  //处理善后工作,资源的关闭
12              System.out.println("finally");
13          }
14      }
15 16      public void c() {
17          d();
18      }
19 20      public void d() {
21          c();
22      }
23  }

程序出现异常 finally

 

允许多个catch ,就像elseif, 要把最大的Throwable写在最下边, 即从小到大

 1  package com.exception;
 2  3  public class Test {
 4      public static void main(String[] args) {
 5          int a = 1;
 6          int b = 0;
 7          try {  //try 监控区域
 8              System.out.println(a / b);
 9          } catch (Error e) {  //catch(想要捕获的异常类型) 捕获异常
10              System.out.println("Error");
11          } catch (Exception e) {  //catch(想要捕获的异常类型) 捕获异常
12              System.out.println("Exception");
13          } catch (Throwable e) {  //catch(想要捕获的异常类型) 捕获异常
14              System.out.println("Throwable");
15          } finally {  //处理善后工作,资源的关闭
16              System.out.println("finally");
17          }
18      }
19 20      public void c() {
21          d();
22      }
23 24      public void d() {
25          c();
26      }
27  }

Exception finally

 

自动生成: ctrl alt + T 在sout后写

 1  package com.exception;
 2  3  public class Test {
 4      public static void main(String[] args) {
 5          int a = 1;
 6          int b = 0;
 7  8          //ctrl  alt  + T
 9          try {
10              System.out.println(a / b);
11          } catch (Exception e) {
12              e.printStackTrace();//打印错误的栈信息,即系统自动生成的  java.lang.ArithmeticException: / by zero
13          } finally {
14          }
15      }
16  }

 

throw主动的抛出异常,一般在方法中使用 ; throws向上抛出(甩锅)

 1 package com.exception;
 2  3  public class Test {
 4      public static void main(String[] args) {
 5          new Test().test(1, 0);
 6      }
 7  8      //throws 假设这个方法中,处理不了这个异常。在方法上抛出异常
 9      public void test(int a, int b) throws ArithmeticException {
10          if (b == 0) {  //throw  throws
11              throw new ArithmeticException();//主动的抛出异常,一般在方法中使用
12          }
13          System.out.println(a / b);
14      }
15  }

 

通过使用try catch 可以捕获异常 , 让程序在不终止的情况下 , 继续执行

 

2. 自定义异常

使用java内置的异常类可以描述在编程中出现的大部分异常情况.

 1  package com.exception;
 2  3  //自定义的异常类
 4  public class MyException extends Exception{
 5  6      //传递数字 >10抛出异常
 7  8      private int detail;
 9 10      public MyException(int a) {  //构造器
11          this.detail = a;
12      }
13 14      //toString:异常的打印信息
15      @Override
16      public String toString() {  //重写
17          return "MyException{" +
18                  "detail=" + detail +
19                  '}';
20      }
21  }
 1 package com.exception;
 2 
 3 public class Test {
 4 
 5     //可能会存在异常的方法
 6 
 7     static void test(int a) throws MyException {
 8         System.out.println("传递的参数为:" + a);
 9         if (a > 10) {
10             throw new MyException(a);//抛出  alt + enter
11         }
12 
13         System.out.println("ok");
14     }
15 
16     public static void main(String[] args) {
17         try {
18             test(1);  //alt  ctrl  t
19         } catch (MyException e) {
20             System.out.println("MyException=>" + e);
21         }
22     }
23 }

传递的参数为:1 ok

传递的参数为:11 MyException=>MyException{detail=11}

 

实际应用中的总结

  1. 处理运行时异常时 , 采用逻辑去合理规避同时辅助 try catch 处理

  2. 在多重catch 块后边 , 可以加上一个 catch(Exception) 来处理可能会被遗漏的异常

  3. 对于不确定的代码 , 也可以加一个try catch , 处理潜在的异常

  4. 尽量去处理异常 , 切勿只是简单的调用 printStackTrace() 去打印输出

  5. 具体如何处理异常 , 要根据不同的业务需求和异常类型去决定

  6. 尽量添加 finally语句块去释放占用的资源 IO 流 Scanner

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多