java try catch throw_Java异常之try,catch,finally,throw,throws

Java异常之try,catch,finally,throw,throws

你能区分异常和错误吗?

我们每天上班,正常情况下可能30分钟就能到达。但是由于车多,人多,道路拥挤,致使我们要花费更多地时间,这就是生活中的异常!

程序和生活一样都会出现异常,先来看个异常:

bc3e158115159ca556fb0a337db5fe74.png

上面出现的是算数错误的异常。

在java中,除去一些编译上的错误(语法)之外,就有异常和错误!

异常的定义是可自己进行处理后,程序依然可以正常运行下去!错误是Java虚拟机抛出的,终止程序的运行,这就是程序和异常的区别。

一:什么是异常处理?

异常处理机制就像我们对平时可能遇到的意外情况,预先想好了一些处理的办法。也就是说,在程序执行代码的时候,万一发生了异常,程序会按照预定的处理办法对异常进行处理,异常处理完毕后,程序继续运行。

java的异常处理是通过5个关键字来实现的:try、catch、finally、throw、throws。

二:java异常类的层次结构

b4cfae850052f94263f9d97a432a91fa.png

三.常见的异常类型

Exception                                          异常层次结构的根类

ArithmeticException                            算数错误情形

ArrayIndexOutOfBoundsException       数组下标越界

NullPointerException                           尝试访问null对象成员

ClassNotFoundException                     不能加载所需的类

InputMismatchException                     欲得到的数据类型与实际输入的类型不匹配

IllegalArgumentException                    方法接受到非法参数

ClassCastException                            对象强制类型转换出错

NumberFormatException                     数字格式转换异常

四.具体实例

try—catch

1 packageTest;2

3 importjava.util.Scanner;4

5 public classTest_Test {6 public static voidmain(String[] args) {7 Scanner input =newScanner(System.in);8 System.out.println("请输入被除数:");9 try{10 int num1=input.nextInt();11 System.out.println("请输入除数:");12 int num2=input.nextInt();13 System.out.println(String.format("%d / %d = %d",14 num1, num2, num1 /num2));15 }catch(Exception e) {16 System.err.println("出现错误:被除数和除数必须是整数,"+

17 "除数不能为零。");18 System.out.println(e.getMessage());19 }20 }

运行结果如下:

f46fb4aae7f492c1ed2d5f5b751789b4.png

System.err.println();这种输出方式可以输出错误的消息,在控制台呈现红色。

System.out用于正常的输出,也就是程序真正想输出的内容。而System.err用于出错信息的输出,也就是你本来不期待看到的东西。

System.out.println(e.getMessage());

这行的作用是——返回该错误的详细信息的字符串。

try-catch-finally

1 packageTest;2

3 importjava.util.Scanner;4

5 public classTest_Test {6 public static voidmain(String[] args) {7 Scanner input =newScanner(System.in);8 System.out.println("请输入被除数:");9 try{10 int num1=input.nextInt();11 System.out.println("请输入除数:");12 int num2=input.nextInt();13 System.out.println(String.format("%d / %d = %d",14 num1, num2, num1 /num2));15 }catch(Exception e) {16 System.err.println("出现错误:被除数和除数必须是整数,"+

17 "除数不能为零。");18 System.out.println(e.getMessage());19 }20 finally{21 System.out.println("Thanks");22 }23 }24 }

运行结果如下:

309bdc42bdb6aa35af702c2a78a97707.png

try-catch-finally 程序块的流程大致分为两种情况:

如果try块中所有语句正常执行完毕,那么finally块就会被执行。

如果try语句在执行过程中碰到异常,无论这种异常能否被catch块捕获到,都将执行finally块中的代码。

try-catch-finally结构中try块是必须有的,catch和finally块为可选,但两者至少必须出现其中之一。

try—catch-catch-finally(多重catch块)

1 packageTest;2

3 importjava.util.InputMismatchException;4 importjava.util.Scanner;5

6 public classTest_Test {7 public static voidmain(String[] args) {8 Scanner input =newScanner(System.in);9 System.out.println("请输入被除数:");10 try{11 int num1=input.nextInt();12 System.out.println("请输入除数:");13 int num2=input.nextInt();14 System.out.println(String.format("%d / %d = %d",15 num1, num2, num1 /num2));16 }catch(InputMismatchException e) {17 System.err.println("被除数和除数必须是整数。");18 }19 catch(ArithmeticException e) {20 System.err.println("除数不能为零。");21 }22 catch(Exception e) {23 System.err.println("其他未知异常。");24 System.out.println(e.getMessage());25 }26 finally{27 System.out.println("Thanks");28 }29 }30 }

运行结果如下:

6899a984d0de66c163b03b17b425ba57.png

所以,在写异常处理的时候,一定要把异常范围小的放在前面,范围大的放在后面,Exception这个异常的根类一定要刚在最后一个catch里面,如果放在前面或者中间,任何异常都会和Exception匹配的,就会报已捕获到...异常的错误。

下面是try-catch-finally中包含return的情况:

情况一:try{} catch(){}finally{} return;

正常按程序顺序执行即可。

1 packageTest;2

3 public classTest_Test {4 public static voidmain(String[] args) {5 Test1();6 }7

8 public static intTest1(){9 int x = 1;10 try

11 {12 x++;13 System.out.println("我有用!");14 }15 catch(Exception e) {16 System.out.println("我没用!");17 }18 finally

19 {20 ++x;21 System.out.println("我也有用!");22 }23 return 2;24 }25 }

运行结果如下:

1aa4446f4b47a3bc49e50ec1551a4339.png

情况2:try{ return; }catch(){} finally{} return;

程序执行try块中return之前(包括return语句中的表达式运算)代码;

再执行finally块,最后执行try中return;

finally块之后的语句return,因为程序在try中已经return所以不再执行。

1 packageTest;2

3 public classTest_Test {4 public static voidmain(String[] args) {5 Test1();6 }7

8 public static intTest1(){9 int x = 1;10 try

11 {12 x++;13 System.out.println("我有用!");14 return 6;15 }16 catch(Exception e) {17 System.out.println("我没用!");18 }19 finally

20 {21 ++x;22 System.out.println("我也有用!");23 }24 return 2;25 }26 }

运行结果如下:

7ae0a7516d7e6d84accd994cc2f84515.png

情况3:try{} catch(){return;} finally{} return;

程序先执行try,如果遇到异常执行catch块,

有异常:则执行catch中return之前(包括return语句中的表达式运算)代码,再执行finally语句中全部代码,

最后执行catch块中return. finally之后也就是4处的代码不再执行。

无异常:执行完try再finally再return.

1.有异常的情况:

1 packageTest;2

3 public classTest_Test {4 public static voidmain(String[] args) {5 Test1();6 }7

8 public static intTest1(){9 int x = 5;10 try

11 {12 int num=x / 0;13 System.out.println(num);14 }15 catch(ArithmeticException e) {16 System.err.println("除数不能为0!");17 return 6;18 }19 finally

20 {21 ++x;22 System.out.println("finally");23 }24 return 2;25 }26 }

运行结果如下:

37866f7bd84e5498520f1e71e3fa8ace.png

2.无异常的情况:

1 packageTest;2

3 public classTest_Test {4 public static voidmain(String[] args) {5 Test1();6 }7

8 public static intTest1(){9 int x = 5;10 try

11 {12 System.out.println("try");13 }14 catch(ArithmeticException e) {15 System.err.println("除数不能为0!");16 return 6;17 }18 finally

19 {20 ++x;21 System.out.println("finally");22 }23 return 2;24 }25 }

运行结果如下:

292eda0bf520aa1233e95f1cfb2593b9.png

情况4:try{ return; }catch(){} finally{return;}

程序执行try块中return之前(包括return语句中的表达式运算)代码;

再执行finally块,因为finally块中有return所以提前退出。

1 packageTest;2

3 public classTest_Test {4 public static voidmain(String[] args) {5 Test1();6 }7

8 public static intTest1(){9 int x = 5;10 try

11 {12 int num = x / 0;13 System.out.println("try");14 return 3;15 }16 catch(ArithmeticException e) {17 System.err.println("除数不能为0!");18 }19 finally

20 {21 ++x;22 System.out.println("finally");23 return 2;24 }25 }26 }

运行结果如下:

96dbcf4a8f50ec9d60d3383d0bd5e9ba.png

情况5:try{} catch(){return;}finally{return;}

程序执行catch块中return之前(包括return语句中的表达式运算)代码;

再执行finally块,因为finally块中有return所以提前退出。

1 packageTest;2

3 public classTest_Test {4 public static voidmain(String[] args) {5 Test1();6 }7

8 public static intTest1(){9 int x = 5;10 try

11 {12 int num = x / 0;13 System.out.println("try");14 }15 catch(ArithmeticException e) {16 System.err.println("除数不能为0!");17 return 4;18 }19 finally

20 {21 ++x;22 System.out.println("finally");23 return 2;24 }25 }26 }

运行结果如下:

35badb34f8fa5f9b429965b606da6975.png

情况6:try{ return;}catch(){return;} finally{return;}

程序执行try块中return之前(包括return语句中的表达式运算)代码;

有异常:执行catch块中return之前(包括return语句中的表达式运算)代码;

则再执行finally块,因为finally块中有return所以提前退出。

无异常:则再执行finally块,因为finally块中有return所以提前退出。

1.有异常

1 packageTest;2

3 public classTest_Test {4 public static voidmain(String[] args) {5 Test1();6 }7

8 public static intTest1(){9 int x = 5;10 try

11 {12 int num = x / 0;13 System.out.println("try");14 return 4;15 }16 catch(ArithmeticException e) {17 System.err.println("除数不能为0!");18 return 4;19 }20 finally

21 {22 ++x;23 System.out.println("finally");24 return 2;25 }26 }27 }

运行结果如下:

8cd46fd7122bba240eddc1253fa3b731.png

2.无异常

1 packageTest;2

3 public classTest_Test {4 public static voidmain(String[] args) {5 Test1();6 }7

8 public static intTest1(){9 int x = 5;10 try

11 {12 //int num = x / 0;13 //System.out.println("try");

14 return 4;15 }16 catch(ArithmeticException e) {17 System.err.println("除数不能为0!");18 return 4;19 }20 finally

21 {22 ++x;23 System.out.println("finally");24 return 2;25 }26 }27 }

运行结果如下:

c3583f299addd88ce23dcbefa40a79fb.png

最终结论:任何执行try 或者catch中的return语句之前,都会先执行finally语句,如果finally存在的话。

如果finally中有return语句,那么程序就return了,所以finally中的return是一定会被return的,

编译器把finally中的return实现为一个warning。

throw——抛出异常

抛出异常有三种形式,一是throw,一个throws,还有一种系统自动抛异常。

系统抛出异常:

1 packageTest;2

3 public classTest2 {4 public static voidmain(String[] args) {5 int a = 5, b =0;6 System.out.println(5/b);7 }8

9 }

运行结果如下:

ac484658d1cd110878663b65d4a7a788.png

throw抛出异常:

throw是语句抛出一个异常。

语法:throw (异常对象);

1 packageTest;2

3 public classTest2 {4 public static voidmain(String[] args) {5 String s = "abc";6 if(s.equals("abc")) {7 throw newNumberFormatException();8 } else{9 System.out.println(s);10 }11 }12

13 }

运行结果如下:

c2e48fcc922ab510ba81b0fe29cb826a.png

throws——声明异常

throws是方法可能抛出异常的声明。(用在声明方法时,表示该方法可能要抛出异常)

语法:[(修饰符)](返回值类型)(方法名)([参数列表])[throws(异常类)]{......}

1 packageTest;2

3 public classTest2 {4 public static voidmain(String[] args) {5 try{6 Test3();7 } catch(NumberFormatException e) {8 System.err.println("非数据类型不能转换。");9 }10 }11

12 public static void Test3() throwsNumberFormatException{13 String s = "abc";14 System.out.println(Double.parseDouble(s));15 }16 }

运行结果如下:

a809bba610e8e39182c3a624ad561f16.png

如果在一个方法体中抛出了异常,那么我们就可以通过throws——声明异常来通知调用者,非常方便。

throws表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某种异常对象。

最后说一句,try-catch-finally虽好用,但是如果是滥用,这样只是会让程序的可读性变的很糟糕,当程序报错,就无法快速准确的定位了,物尽其用 人尽其才嘛!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值