java 异常处理(二)

来源:互联网 发布:java审批流 编辑:程序博客网 时间:2024/06/04 18:34

一、java异常处理语法

1、在Java程序中,可以通过try语句来捕获异常,catch语句来处理异常,其语法如下

try{//业务逻辑代码}catch(异常类名 参数名){//异常处理代码} 
2、多重catch语句:在一个try语句块后可跟多个catch语句块,不同的catch语句用于处理不同类型的异常。

    Note:子异常类处理程序应放在父异常类成立程序之前,否则提示报错。

try{//业务逻辑代码}catch(异常类名1 参数名1){//异常处理代码1}catch(异常类名2 参数名2){//异常处理代码2}……catch(异常类名n 参数名n){//异常处理代码n}
3、finally语句:每一个try语句可以有一个可选的finally语句,无论try语句块或catch语句块是否产生异常,finally语句块都会被执行。finally可用于回收资源或保证特定代码被执行。

4、try语句(非try-with)后须紧跟catch或finally语句,且当同时有两者时,finally语句须位于所有catch语句之后。可以表示为:try  catch* (catch | finally)。安静 

5、在异常处理中,可以使用嵌套的异常处理结构。可以在try语句、catch语句、finally语句中嵌套try/catch/finally组合。

Note:try语句块内部代码执行效率比较低,因此尽量把可能会产生异常的代码书写在try语句块内部。

二、异常捕获的重要性

      当应用程序出现异常时,尽可能进行异常捕获,一方面防止程序异常结束;另一方面在出现异常时给出用户看得懂的提示信息。

三、异常处理流程

1、程序无异常

     执行try语句代码;

1.1 带有finally语句

     执行finally语句块代码,然后执行try-catch-finally后的代码。

public class Demo{public static void main (String[] args) {try{int i = Integer.parseInt("10");}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}finally{System.out.println("finally statement.");}System.out.println("statment out of try-catch-finally.");}}执行结果:finally statement.statment out of try-catch-finally.

1.2 无finally语句

      直接执行try-catch-finally后的代码。

public class Demo{public static void main (String[] args) {try{int i = Integer.parseInt("10");}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}System.out.println("statment out of try-catch-finally.");}}执行结果:statment out of try-catch-finally.

2、程序中出现异常

2.1 捕获该异常

   首先执行该异常对应处理程序;

   2.1.1 带有finally语句

           执行finally语句块语句,然后执行try-catch-finally后的语句块。

public class Demo{public static void main (String[] args) {try{int i = Integer.parseInt("10a");}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}finally{System.out.println("finally statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果:NumberFormatException statement.finally statement.statement out of try-catch-finally.

   2.1.2 无finally语句

           执行完catch语句后,直接执行try-catch后的代码。

public class Demo{public static void main (String[] args) {try{int i = Integer.parseInt("10a");}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果为:NumberFormatException statement.statement out of try-catch-finally.

2.2没有捕获该异常

   2.2.1 带有finally语句

           执行finally语句,然后向调用者抛出该异常。

public class Demo{public static void main (String[] args) {try{int i = Integer.parseInt("10")/0;}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}finally{System.out.println("finally statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果:finally statement.Exception in thread "main" java.lang.ArithmeticException: / by zero    at Demo.main(Demo.java:5)

   2.2.2 无finally语句

            直接向调用者抛出该异常。

public class Demo{public static void main (String[] args) {try{int i = Integer.parseInt("10")/0;}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果:Exception in thread "main" java.lang.ArithmeticException: / by zero    at Demo.main(Demo.java:5)

 3、try或catch语句中带有return/throw语句

   3.1 带有finally语句

         先执行finally语句块程序,然后返回到return/throw处,结束此方法执行。

return语句demo:
public class Demo{public static void main (String[] args) {try{int i = Integer.parseInt("10");return;}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}finally{System.out.println("finally statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果:finally statement.
throw语句demo:
public class Demo{public static void main (String[] args) throws Exception{//自己不处理,抛出异常try{int i = Integer.parseInt("10");throw new Exception("故意抛出异常……");}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}finally{System.out.println("finally statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果(参照2.2.1,抛出Exception异常,处理不了哦;反之抛出NumberFormatException,变成2.1.1):finally statement.Exception in thread "main" java.lang.Exception: 故意抛出异常……at Demo.main(Demo.java:6)public class Demo{public static void main (String[] args) throws Exception{try{int i = Integer.parseInt("10");throw new NumberFormatException("故意抛出异常……");}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}finally{System.out.println("finally statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果:NumberFormatException statement.finally statement.statement out of try-catch-finally.

3.2 无finally语句

     直接结束此方法执行。 

public class Demo{public static void main (String[] args) throws Exception{try{int i = Integer.parseInt("10");throw new NumberFormatException("故意抛出异常……");}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果:NumberFormatException statement.statement out of try-catch-finally.

4、try或catch语句中带有System.exit()语句

     无论是否带有finally语句均直接结束所有的代码执行。

public class Demo{public static void main (String[] args) throws Exception{try{int i = Integer.parseInt("10");System.out.println("Before exit……");System.exit(0);}catch(NumberFormatException nfe){System.out.println("NumberFormatException statement.");}finally{System.out.println("finally statement.");}System.out.println("statement out of try-catch-finally.");}}执行结果(finally没有执行哦):Before exit……

四、参考文献

1、异常(try...catch...finally、throws、throw)

2、java 异常: try...catch...finally