异常: 用于处理程序中错误的一种机制
1. 捕获和处理异常:
try...catch...finally
我们直接上代码:
1 package com.learn.chap04.sec02; 2 3 public class Demo1 { 4 public static void main(String[] args) { 5 String str = "123q"; 6 // 捕获和处理异常 7 try{ 8 int a = Integer.parseInt(str); 9 }catch(NumberFormatException e){10 //e.getMessage(); // 打印日志11 e.printStackTrace();12 }catch(Exception e){ // catch中 范围依次从小到大,NumberFormatException和Exception交换位置后,将报错13 e.printStackTrace();14 }15 16 System.out.println("aa"); // 由于上面代码做了异常捕捉和处理,并且没有return,所以此处将被打印出来17 18 }19 }
运行结果
aa
java.lang.NumberFormatException: For input string: "123q" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at com.learn.chap04.sec02.Demo1.main(Demo1.java:8)
1 package com.learn.chap04.sec02; 2 3 public class Demo2 { 4 5 public static void testFinally(){ 6 String str = "123a"; 7 try { 8 int a = Integer.parseInt(str); 9 System.out.println(a);10 } catch (Exception e) {11 e.printStackTrace();12 System.out.println("exception");13 return; // 下面的代码System.out.println("end");将不执行 14 }finally{ // 无论有没有异常,try...catch中有没有return,finally里的代码 都会被执行;这就是finally的作用。这个作用比喻:可在程序运行后关闭文件,不管运行结果如何都关闭文件,就可以用这一条语句。但可以用exit函数退出再不执行finally语句。比喻将return换成System.exit(0);就可以不执行15 System.out.println("finally end");16 }17 System.out.println("end"); // 由于上面代码出现return,所以此处将无法被打印出来18 }19 20 public static void main(String[] args) {21 testFinally();22 }23 }
运行结果
java.lang.NumberFormatException: For input string: "123a"
at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at com.learn.chap04.sec02.Demo2.testFinally(Demo2.java:8) at com.learn.chap04.sec02.Demo2.main(Demo2.java:21)exceptionfinally end
2.throws和throw关键字:
throws 表示当前方法不处理异常,而是交给方法的调用处去处理;
throw 表示直接抛出一个异常
我们直接上代码:
1 package com.learn.chap04.sec03; 2 3 public class Demo1 { 4 5 /** 6 * 把异常向外面抛 7 * @throws Exception 8 */ 9 public static void testThrows() throws Exception{10 String str = "123a";11 int a = Integer.parseInt(str);12 System.out.println(a);13 }14 15 /*public static void main(String[] args) {16 testThrows();17 }*/18 public static void main(String[] args) {19 try {20 testThrows();21 System.out.println("here");22 } catch (Exception e) {23 System.out.println("我们在这里抛出了异常");24 e.printStackTrace();25 }26 System.out.println("输出内容");27 }28 29 }
运行结果
我们在这里抛出了异常
java.lang.NumberFormatException: For input string: "123a" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at com.learn.chap04.sec03.Demo1.testThrows(Demo1.java:11) at com.learn.chap04.sec03.Demo1.main(Demo1.java:20)输出内容
1 package com.learn.chap04.sec03; 2 3 public class Demo2 { 4 5 public static void testThrow(int a) throws Exception{ 6 if(a==1){ 7 throw new Exception(a+"有异常"); 8 } 9 System.out.println(a+"无异常");10 }11 12 public static void main(String[] args) {13 try {14 testThrow(1);15 } catch (Exception e) {16 e.printStackTrace();17 }18 19 try {20 testThrow(0);21 } catch (Exception e) {22 e.printStackTrace();23 }24 }25 }
运行结果
java.lang.Exception: 1有异常
at com.learn.chap04.sec03.Demo2.testThrow(Demo2.java:7) at com.learn.chap04.sec03.Demo2.main(Demo2.java:14)0无异常3.Exception与RuntimeException的区别:
我们直接上代码
1 package com.learn.chap04.sec04; 2 3 public class Demo1 { 4 5 /** 6 * 运行时异常,编译时不检查,可以不使用try..catch捕获 7 */ 8 public static void testRuntimeException() throws RuntimeException{ 9 throw new RuntimeException("运行时异常");10 }11 12 /**13 * Exception异常,编译时会检查,必须使用使用try..catch捕获14 */15 public static void testException() throws Exception{16 throw new Exception("Exception异常");17 }18 19 public static void main(String[] args) {20 //testRuntimeException();21 try {22 testRuntimeException();23 } catch (Exception e) {24 // TODO: handle exception25 e.printStackTrace();26 }// 此处若不加try...catch 由于出现异常,下面代码将不执行27 28 try {29 testException();30 } catch (Exception e) {31 // TODO Auto-generated catch block32 e.printStackTrace();33 }34 }35 }
运行结果
java.lang.RuntimeException: 运行时异常
at com.learn.chap04.sec04.Demo1.testRuntimeException(Demo1.java:9) at com.learn.chap04.sec04.Demo1.main(Demo1.java:22)java.lang.Exception: Exception异常 at com.learn.chap04.sec04.Demo1.testException(Demo1.java:16) at com.learn.chap04.sec04.Demo1.main(Demo1.java:29)
4. 自定义异常类
直接上代码
1 package com.learn.chap04.sec05; 2 /** 3 * 自定义异常 继承Exception 4 * @author eagle 5 * 6 */ 7 public class CustomException extends Exception { 8 public CustomException(String message){ 9 super(message);10 }11 }
1 package com.learn.chap04.sec05; 2 3 public class TestCustomException { 4 public static void test() throws CustomException{ 5 throw new CustomException("自定义异常"); 6 } 7 8 public static void main(String[] args) { 9 try {10 test();11 } catch (Exception e) {12 // TODO: handle exception13 e.printStackTrace();14 }15 }16 }
运行结果
com.learn.chap04.sec05.CustomException: 自定义异常
at com.learn.chap04.sec05.TestCustomException.test(TestCustomException.java:5) at com.learn.chap04.sec05.TestCustomException.main(TestCustomException.java:10)