动手动脑——异常处理

Java中实现异常处理的基础知识

捕捉异常:

try{
//可能发生运行错误的代码;
}catch(异常类型 异常对象引用){ //用于处理异常的代码 } finally{ //用于“善后” 的代码 }

  Throwable类是Java异常类型的顶层父类。Java标准库内建了一些通用的异常,这些类以Throwable为顶层父类。Throwable又派生出Error类和Exception类。错误:Error类以及他的子类的实例,代表了JVM本身的错误。错误不能被程序员通过代码处理,Error很少出现。异常:Exception以及他的子类,代表程序运行时发送的各种不期望发生的事件。Java 中所有可捕获的异常都派生自 Exception 类。

  可捕获的异常又可以分为两类: (1)Check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出 (2)Runtime异常:派生自RuntimeException的异常类。使用throw语句可以随时抛出这种异常对象: throw new ArithmeticException(…);

  把可能会发生错误的代码放进try语句块中,catch语句块中的代码用于处理错误。 当异常发生时,程序控制流程由try语句块跳转到catch语句块。 不管是否有异常发生,finally语句块中的语句始终保证被执行。 如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

多层的异常捕获

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(); 
        } 
        catch(ArithmeticException e) { 
            System.out.println("发生ArithmeticException"); 
        } 
        catch(ArrayIndexOutOfBoundsException e) { 
           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
        } 
    } 
}

  内层try抛出第一个异常时,程序控制流程跳转到内层的catch语句,解决掉了抛出的第一个异常,此时继续执行外层的try语句块内容,抛出了第二个异常(ArithmeticException),此时按异常类型选择执行catch语句块内容。

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"); 
        } 
    } 
}

  内层try抛出异常后,内层的catch语句块无法解决抛出的异常,所以跳转到了外层的catch语句块,解决完异常后没有了后续的语句,所以程序结束。

try···catch···finally嵌套使用问题

public class EmbededFinally {
    public static void main(String args[]) {
        int result;
        try {
  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"); } // result = 100 / 0; //level 1 } catch (Exception e) { System.out.println("Level 1:" + e.getClass().toString()); } finally { System.out.println("In Level 1 finally"); } } }

  首先执行第一个try内容,输出“in Level 1”,然后执行第二个try内容,输出“in Level 2”,然后执行第三个try内容,输出“in Level 3”并抛出了异常ArithmeticException,此时执行紧跟着的catch语句块内容,解决掉了抛出的异常。此时,所有try语句内容结束,然后按先后顺序依次执行finally的内容。

finally语句块一定会执行吗

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

  当程序退出后,即System.exit(0);语句执行后,finally语句块的内容就不会再执行了。

原文地址:https://www.cnblogs.com/dream0-0/p/9945785.html