动手动脑第五波异常处理

it2025-12-05  13

 

 

注意:

1.可以有多个catch语句块,每个代码块捕获一种异常。在某个try块后有两个不同的catch 块捕获两个相同类型的异常是语法错误。2.使用catch语句,只能捕获Exception类及其子类的对象。因此,一个捕获Exception对象的catch语句块可以捕获所有“可捕获”的异常。3.将catch(Exception e)放在别的catch块前面会使这些catch块都不执行,因此Java不会编译这个程序。

4.

package 异常处理; public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); } throw new ArithmeticException(); //throw new ArrayIndexOutOfBoundsException(); } catch(ArithmeticException e) { System.out.println("发生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); } } }

5.

package 异常处理; public class CatchWho2 { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArithmeticException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); } throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("发生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); } } }

结果分析:代码一:当内层捕获异常并处理后,外层则不再捕获该异常。

代码二:当异常未被处理时无法接受新的异常。

JAVA语言提供两种异常处理机制:捕获异常和声明抛弃异常

1)捕获异常:在Java程序运行过程中系统得到一个异常对象是,它将会沿着方法的调用栈逐层回溯,寻找处理这一异常的代码。找到能够处理这种类型异常的方法后,运行时系统把当前异常交给这个方法处理;如果找不到可以捕获异常的方法,则运行时系统将终止,相应的Java程序也将退出。

2)声明抛弃异常:当Java程序运行时系统得到一个异常对象时,如果一个方法并不知道如何处理所出现的异常,则可在方法声明时,声明抛弃异常。声明抛弃异常是在一个方法声明中的throws子句中指明的。

6.

异常处理:Java中异常捕获语句

try{用于监控可能发生错误的语句}

catch(异常类型 异常对象引用)

{ 用于捕获并处理异常的代码 }

finally

{ //用于“善后” 的代码 }

不管是否有异常发生,finally语句块中的语句始终保证被执行。

7.

package 异常处理; public class EmbededFinally { public static void main(String args[]) { int result; try { //result=100/0; System.out.println("in Level 1"); try { System.out.println("in Level 2"); result=100/0; //Level 2 try { System.out.println("in Level 3"); //result=100/0; //Level 3 } catch (Exception e) { System.out.println("Level 3:" + e.getClass().toString()); } finally { System.out.println("In Level 3 finally"); } //result=100/0; //Level 2 } catch (Exception e) { System.out.println("Level 2:" + e.getClass().toString()); } finally { System.out.println("In Level 2 finally");A } catch (Exception e) { System.out.println("Level 1:" + e.getClass().toString()); } finally { System.out.println("In Level 1 finally"); } } }

当外层异常未被处理时,内层异常不会被处理而且finally也不会执行,当有多层嵌套的finally语句时,异常在不同层次不同位置抛出时,也会导致不同的finally语句块执行顺序。

8.

package 异常处理; public class SystemExitAndFinally { public static void main(String[] args) { try{ System.out.println("in main"); throw new Exception("Exception is thrown in main"); } catch(Exception e) { System.out.println(e.getMessage()); // System.exit(0); } finally { System.out.println("in finally"); } } }

 

结果分析:只有与finally对应的try语句得到执行的情况下finally语句才会执行,但如果finally语句之前出现例如System.exit(0) 等使Java虚拟机停止运行的语句时finally语句也不会被执行。

9.printStackTrace:打印方法调用堆栈。每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

10.throws语句表明某方法中可能出现某种(或多种)异常,但它自己不能处理这些异常,而需要由调用者来处理。当一个方法包含throws子句时,需要在调用此方法的代码中使用try/catch/finally进行捕获,或者是重新对其进行声明,否则编译时报错。

11.自行归纳Java多层嵌套异常处理的基本流程

在java语言中,通常将可能出现异常的语句放入try{}语句中,将出现错误后需要执行的语句放入到catch{}语句中,将无论是否发生异常都要执行的语句放在finally{}语句中。当程序执行出现异常的时候,系统会抛出一个异常,然后由try{}语句中中出现异常的地方转到catch{}语句中。不过不管有没有异常产生,finally{}中的语句都将执行。如果系统出现系统错误或者运行Runtime异常,jvm会结束程序运行,不一定会执行finally{}中的语句。如果try{}中产生的异常在catch中没有处理,系统将停止程序,也不会执行finally中的语句。

12.子类方法不能声明比父类同名更大的异常否则代码无法编译

一个子类的throws子句抛出的异常,不能是其基类同名方法抛出的异常对象的父类。

13.Java 7 及以后的版本,允许在一个catch块中捕获多个异常。示例代码如下:

try {   //...   throw new SocketException(); } catch (SocketException | SecurityException | NullPointerException e) {   //exception handler }

14.自定义异常通常选择直接派生自Exception:

Class MyException extends Exception { …    }

在合适的地方使用throw语句抛出自定义异常对象:

Class MyClass {  void someMethod() {   if (条件) throw new MyException();  } }

 

 

 

 

 

转载于:https://www.cnblogs.com/zzstdruan1707-4/p/9930441.html

相关资源:数据结构—成绩单生成器
最新回复(0)