java捕获异常number_Java之异常处理

异常: 用于处理程序中错误的一种机制

1. 捕获和处理异常:

try...catch...finally

我们直接上代码:

1 packagecom.learn.chap04.sec02;2

3 public classDemo1 {4 public static voidmain(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 packagecom.learn.chap04.sec02;2

3 public classDemo2 {4

5 public static voidtestFinally(){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 voidmain(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)

exception

finally end

2.throws和throw关键字:

throws 表示当前方法不处理异常,而是交给方法的调用处去处理;

throw 表示直接抛出一个异常

我们直接上代码:

1 packagecom.learn.chap04.sec03;2

3 public classDemo1 {4

5 /**

6 * 把异常向外面抛7 *@throwsException8 */

9 public static void testThrows() throwsException{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 voidmain(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 packagecom.learn.chap04.sec03;2

3 public classDemo2 {4

5 public static void testThrow(int a) throwsException{6 if(a==1){7 throw new Exception(a+"有异常");8 }9 System.out.println(a+"无异常");10 }11

12 public static voidmain(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的区别:

1435b4f9fc8caf41d701d2ad42974d23.png

我们直接上代码

1 packagecom.learn.chap04.sec04;2

3 public classDemo1 {4

5 /**

6 * 运行时异常,编译时不检查,可以不使用try..catch捕获7 */

8 public static void testRuntimeException() throwsRuntimeException{9 throw new RuntimeException("运行时异常");10 }11

12 /**

13 * Exception异常,编译时会检查,必须使用使用try..catch捕获14 */

15 public static void testException() throwsException{16 throw new Exception("Exception异常");17 }18

19 public static voidmain(String[] args) {20 //testRuntimeException();

21 try{22 testRuntimeException();23 } catch(Exception e) {24 //TODO: handle exception

25 e.printStackTrace();26 }//此处若不加try...catch 由于出现异常,下面代码将不执行

27

28 try{29 testException();30 } catch(Exception e) {31 //TODO Auto-generated catch block

32 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 packagecom.learn.chap04.sec05;2 /**

3 * 自定义异常 继承Exception4 *@authoreagle5 *6 */

7 public class CustomException extendsException {8 publicCustomException(String message){9 super(message);10 }11 }

1 packagecom.learn.chap04.sec05;2

3 public classTestCustomException {4 public static void test() throwsCustomException{5 throw new CustomException("自定义异常");6 }7

8 public static voidmain(String[] args) {9 try{10 test();11 } catch(Exception e) {12 //TODO: handle exception

13 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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值