异常处理
一、关键字
1.1 throw关键字
1 package com; 2 //继承Exception的类是异常类 3 public class MyException extends Exception { 4 String error; 5 MyException(){ 6 7 } 8 MyException(String exception){ 9 this.error = exception; 10 // System.out.println("不好意思,发现"+error+"错误,请告诉管理员"); 11 } 12 }
1.2 try--catch--finally--throws关键字
1 package com; 2 3 public class Test { 4 public static void testColor(){ 5 //颜色#aaa 和#aaaaaa 6 //也就是说可以添加括号,表示一群字符串是一个整体,运算时候方便 7 //|的优先级比较低 8 // String p = "#[0-9a-f]{3}|[0-9a-f]{6}"; 9 String p = "#([0-9a-f]{3}|[0-9a-f]{6})"; 10 System.out.println("#3fffff".matches(p)); 11 } 12 13 //顺序结构,有异常、return退出 14 public static int testTry1(){ 15 int i = 0; 16 int m = 10; 17 try { 18 System.out.println(m/i); 19 }catch (ArithmeticException e){ 20 System.out.println("算术异常,0不能做除数"); 21 return i; 22 } 23 catch (Exception e){ 24 System.out.println("未知异常,不能处理"); 25 } 26 finally { 27 i++; 28 return i; 29 } 30 31 } 32 //顺序结构,没有异常,return退出 33 public static int testTry2(){ 34 System.out.println("顺序结构没有异常,return退出"); 35 int i = 0; 36 int m = 10; 37 try { 38 i++; 39 System.out.println(m/i); 40 return i; 41 }catch (ArithmeticException e){ 42 System.out.println("算术异常,0不能做除数"); 43 } 44 catch (Exception e){ 45 System.out.println("未知异常,不能处理"); 46 } 47 finally { 48 i++; 49 return i; 50 } 51 52 } 53 //顺序结构,没有异常 54 public static int testTry3(){ 55 System.out.println("顺序结构,没有异常,没有退出"); 56 int i = 0; 57 int m = 10; 58 try { 59 i++; 60 System.out.println(m/i); 61 }catch (ArithmeticException e){ 62 System.out.println("算术异常,0不能做除数"); 63 } 64 catch (Exception e){ 65 System.out.println("未知异常,不能处理"); 66 } 67 finally { 68 i++; 69 return i; 70 } 71 } 72 73 74 //break类似return 75 76 public static int testTry4(){ 77 System.out.println("循环结构,存在异常,break退出。"); 78 int num = 0; 79 for (int i = 4;i>=0;i-=2){ 80 try { 81 System.out.println("i="+i+",10/i)="+(10/i)); 82 } 83 catch (ArithmeticException e){ 84 System.out.println("算术异常0不能做除数"); 85 break;//break会跳过这一次的i--,但是continue不会 86 } 87 finally { 88 num = ++i;//每次循环必须执行,所以是i--死循环 89 } 90 } 91 return num; 92 } 93 94 //for循环continue比较特别,不会立刻结束,而是等finally执行完了再执行自增/自减,然后条件判断, 95 public static int testTry5(){ 96 System.out.println("循环结构,存在异常,continue结束这次循环退出。"); 97 int num = 0; 98 int i; 99 for (i = 10;i>=0;i-=2){ 100 try { 101 System.out.println("i="+i+",10/i)="+(10/i)); 102 } 103 catch (ArithmeticException e){ 104 System.out.println(i); 105 System.out.println("算术异常0不能做除数"); 106 continue;//break会跳过这一次的i--,但是continue不会 107 } 108 finally { 109 System.out.println(++i); 110 } 111 //下面这句话很关键,还有语句呢 112 System.out.println("哈哈,continue没有执行"); 113 } 114 System.out.println(i); 115 num = i; 116 return num; 117 } 118 119 static void testContinue(){ 120 for (int i = 10;i>=0;i-=2){ 121 if(i==8){ 122 continue; 123 } 124 System.out.println(i); 125 } 126 } 127 128 static void myException1(String msg){ 129 if(msg==null){ 130 throw new RuntimeException("空指针异常"); 131 } 132 } 133 //我的一场必须把它抛出来 134 static void myException2(String msg) throws MyException{ 135 if(msg==null){ 136 throw new MyException("空指针异常"); 137 } 138 } 139 140 public static void testException() { 141 // int num = testTry5(); 142 // System.out.println(num); 143 144 try { 145 myException2(null); 146 }catch (MyException e){ 147 System.out.println("哈哈,异常被发现了"); 148 } 149 catch (Exception e){ 150 System.out.println("未知异常,不能处理"); 151 } 152 myException1(null);//系统异常,默认直接终止了 153 //自定义异常,实例化+catch 过程中若没有return 就不会终止。 154 } 155 156 public static void main(String[] args) { 157 //测试final, finalize 158 //final是关键字 159 // 修饰局部变量,类,方法,成员属性 160 // final String name = "zhangsan";//局部变量 161 162 TestFinalize test = new TestFinalize(); 163 test = null; //必须不用才能让jvm清理。 164 System.gc(); 165 try { 166 Thread.sleep(1000); 167 } catch (InterruptedException e) { 168 System.out.println("睡眠异常"); 169 } 170 catch (Exception e){ 171 System.out.println("未知异常"); 172 } 173 // System.sleep(); 174 } 175 static final String pet = "dog"; 176 final int pf(int t){ 177 return t*t; 178 } 179 //可以重载 180 final long pf(long t){ 181 return t*t; 182 } 183 } 184 185 final class Animal extends Test{ 186 //不能重写/覆盖最终方法 ---保护,不能覆盖但是可以用/读 187 //java认为方法重载不是修改,方法覆盖/重写是修改 188 // @Override 189 // final int pf(int t){ 190 // return t; 191 // } 192 193 } 194 195 ////不能继承最终类 196 //class Dog extends Animal{ 197 // 198 //}
二、final,finally,finalize关键字
2.1finalize关键字
1 package com; 2 3 public class TestFinalize { 4 //重写一下Object的finalize方法,finalize不是关键字 5 AAAA a = new AAAA(); 6 @Override 7 protected void finalize() throws Throwable { 8 super.finalize(); 9 System.out.println("请放心,TestFinalize的一个实例对象已经被自动释放"); 10 } 11 } 12 13 class BBBB { 14 protected void finalize() throws Throwable { 15 super.finalize(); 16 System.out.println("请放心,BBBB的一个实例对象已经被自动释放"); 17 } 18 } 19 20 class AAAA extends BBBB{ 21 protected void finalize() throws Throwable { 22 super.finalize();//清空父类 23 System.out.println("请放心,AAAA的一个实例对象已经被自动释放"); 24 } 25 } 26 27 //finalize, 28 //1.在不用的情况下jvm可以回收 29 //2.可以通过System.in方法手动回收,也可让jvm自动回收 30 //3.回收的之前的某一时刻就是调用了finalize()方法, 31 //4.回收不是程序员干的。 32 //5.应该把父类的finalzie()方法也调用了,万一它在回收前需要执行代码,
【推荐】FlashTable:表单开发界的极速跑车,让你的开发效率一路狂飙
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步