ToB企服应用市场:ToB评测及商务社交产业平台

标题: 【狂神说Java】Java零基础学习笔记-异常 [打印本页]

作者: 宁睿    时间: 2023-8-29 12:19
标题: 【狂神说Java】Java零基础学习笔记-异常
【狂神说Java】Java零基础学习笔记-异常

异常01:Error和Exception

什么是异常

简单分类

异常体系结构


Error

Exception

  1. package com.exception;
  2. public class Demo01 {
  3.     public static void main(String[] args) {
  4. //        new Demo01().a();
  5. //        System.out.println(11/0);
  6. //        System.out.println()
  7.     }
  8. //    public void a(){
  9. //        b();
  10. //    }
  11. //
  12. //    public void b(){
  13. //        a();
  14. //    }
  15. }
复制代码
异常02:捕获和抛出异常

异常处理机制

  1. package com.exception;
  2. public class Test {
  3.     public static void main(String[] args) {
  4.         try {
  5.             new Test().test(1,0);
  6.         } catch (ArithmeticException e) {
  7.             e.printStackTrace();
  8.         }
  9.     }
  10.     //假设这方法中,处理不了这个异常。方法上抛出异常
  11.     public void test(int a,int b) throws ArithmeticException{
  12.         if (b==0){ //throw throws
  13.             throw new ArithmeticException(); //主动的抛出异常,一般在方法中使用
  14.         }
  15. //        System.out.println(a/b);
  16.     }
  17. }
  18. /*
  19.     public static void main(String[] args) {
  20.         int a = 1;
  21.         int b = 0;
  22.         //假设要捕获多个异常:从小到大!
  23.         try { //try监控区域
  24.             if (b==0){ //throw throws
  25.                 throw new ArithmeticException(); //主动的抛出异常
  26.             }
  27.             System.out.println(a/b);
  28. //            new Test().a();
  29.         }catch (Error e){ //catch(想要捕获的异常类型!)捕获异常
  30. //        }catch (Throwable e){ //catch(想要捕获的异常类型!)捕获异常
  31. //            System.out.println("程序出现异常,变量b不能为0");
  32.             System.out.println("Error");
  33.         }catch (Exception e){
  34.             System.out.println("Exception");
  35.         }catch (Throwable t){
  36.             System.out.println("Throwable");
  37.         } finally { //finally处理善后工作
  38.             System.out.println("finally");
  39.         }
  40.         //finally 可以不要finally,假设IO,资源,关闭!
  41.     }
  42. //    public void a(){
  43. //        b();
  44. //    }
  45. //
  46. //    public void b(){
  47. //        a();
  48. //    }
  49. */
复制代码
  1. package com.exception;
  2. public class Test2 {
  3.     public static void main(String[] args) {
  4.         int a = 1;
  5.         int b = 0;
  6.         //ctrl + alt + T
  7.         try {
  8.             System.out.println(a/b);
  9.         } catch (Exception e) {
  10.             e.printStackTrace(); //打印错误的栈信息
  11.         } finally {
  12.         }
  13.     }
  14. }
复制代码
异常03:自定义异常及经验小结

自定义异常

实际应用中的经验总结

  1. package com.exception.demo02;
  2. //自定义的异常类
  3. public class MyException extends Exception{
  4.     //传递数字>10;
  5.     private int detail;
  6.     public MyException(int a) {
  7.         this.detail = a;
  8.     }
  9.     //toString:异常的打印信息
  10.     @Override
  11.     public String toString() {
  12.         return "MyException{" +
  13.                 "detail=" + detail +
  14.                 '}';
  15.     }
  16. }
复制代码
  1. package com.exception.demo02;
  2. public class Test {
  3.     //可能会存在异常的方法
  4.     static void test(int a) throws MyException {
  5.         System.out.println("传递的参数为:"+a);
  6.         if (a>10){
  7.             throw new MyException(a); //抛出
  8.         }
  9.         System.out.println("OK");
  10.     }
  11.     public static void main(String[] args) {
  12.         try {
  13.             test(11);
  14.         } catch (MyException e) {
  15.             System.out.println("MyException=>"+e);
  16.         }
  17.     }
  18. }
复制代码
【【狂神说Java】Java零基础学习视频通俗易懂】https://www.bilibili.com/video/BV12J41137hu?p=77&vd_source=fd5defd6aaa826e3b6e0c06073353e32


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4