📜  Java中 try catch finally 中的流控制

📅  最后修改于: 2022-05-13 01:54:57.674000             🧑  作者: Mango

Java中 try catch finally 中的流控制

在本文中,我们将探讨在引发异常时可能发生的所有可能的 try-catch-finally 组合,以及在每种给定情况下控制流如何发生。

  1. try-catch 子句或 try-catch-finally 子句中的控制流
    • 案例一: try块发生异常,catch块处理
    • 情况 2: try-block 发生异常,catch 块没有处理
    • 案例3: try-block中没有出现异常
  2. try-finally 子句
    • 案例一: try块出现异常
    • 案例2: try-block中没有出现异常

try-catch 或 try-catch-finally 中的控制流

1、异常发生在try块中,在catch块中处理:如果try块中的语句引发异常,则try块的其余部分不执行,控制权交给相应的catch块。执行 catch 块后,控制权将转移到 finally 块(如果存在),然后执行其余程序。

  • try-catch 中的控制流:
Java
// Java program to demonstrate
// control flow of try-catch clause
// when exception occur in try block
// and handled in catch block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
        catch(ArrayIndexOutOfBoundsException ex)
        {
            System.out.println("Exception caught in Catch block");
        }
         
        // rest program will be executed
        System.out.println("Outside try-catch clause");
    }
}


Java
// Java program to demonstrate
// control flow of try-catch-finally clause
// when exception occur in try block
// and handled in catch block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
         
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        catch(ArrayIndexOutOfBoundsException ex)
        {
            System.out.println("Exception caught in catch block");
        }
         
        finally
        {
            System.out.println("finally block executed");
        }
         
        // rest program will be executed
        System.out.println("Outside try-catch-finally clause");
    }
}


Java
// Java program to demonstrate
// control flow of try-catch clause
// when exception occurs in try block
// but not handled in catch block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        // not a appropriate handler
        catch(NullPointerException ex)
        {
            System.out.println("Exception has been caught");
        }
         
        // rest program will not execute
        System.out.println("Outside try-catch clause");
    }
}


Java
// Java program to demonstrate
// control flow of try-catch-finally clause
// when exception occur in try block
// but not handled in catch block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
         
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        // not a appropriate handler
        catch(NullPointerException ex)
        {
            System.out.println("Exception has been caught");
        }
         
        finally
        {
            System.out.println("finally block executed");
        }
         
        // rest program will not execute
        System.out.println("Outside try-catch-finally clause");
    }
}


Java
// Java program to demonstrate try-catch
// when an exception doesn't occurred in try block
class GFG
{
    public static void main (String[] args)
    {
         
        try
        {
             
            String str = "123";
                 
            int num = Integer.parseInt(str);
                 
            // this statement will execute
            // as no any exception is raised by above statement
            System.out.println("Inside try block");
             
        }
         
        catch(NumberFormatException ex)
        {
                 
            System.out.println("catch block executed...");
                 
        }
 
        System.out.println("Outside try-catch clause");
    }
}


Java
// Java program to demonstrate try-catch-finally
// when exception doesn't occurred in try block
class GFG
{
    public static void main (String[] args)
    {
         
    try
    {
         
        String str = "123";
             
        int num = Integer.parseInt(str);
             
        // this statement will execute
        // as no any exception is raised by above statement
        System.out.println("try block fully executed");
         
    }
     
    catch(NumberFormatException ex)
    {
             
        System.out.println("catch block executed...");
             
    }
     
    finally
    {
        System.out.println("finally block executed");
    }
 
    System.out.println("Outside try-catch-finally clause");
    }
}


Java
// Java program to demonstrate
// control flow of try-finally clause
// when exception occur in try block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        finally
        {
            System.out.println("finally block executed");
        }
         
        // rest program will not execute
        System.out.println("Outside try-finally clause");
    }
}


Java
// Java program to demonstrate
// control flow of try-finally clause
// when exception doesn't occur in try block
class GFG
{
    public static void main (String[] args)
    {
         
        try
        {
            String str = "123";
             
            int num = Integer.parseInt(str);
             
            // this statement will execute
            // as no any exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        finally
        {
            System.out.println("finally block executed");
        }
         
        // rest program will be executed
        System.out.println("Outside try-finally clause");
    }
}


输出:

Exception caught in Catch block
Outside try-catch clause
  • try-catch-finally 子句中的控制流:

Java

// Java program to demonstrate
// control flow of try-catch-finally clause
// when exception occur in try block
// and handled in catch block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
         
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        catch(ArrayIndexOutOfBoundsException ex)
        {
            System.out.println("Exception caught in catch block");
        }
         
        finally
        {
            System.out.println("finally block executed");
        }
         
        // rest program will be executed
        System.out.println("Outside try-catch-finally clause");
    }
}

输出:

Exception caught in catch block
finally block executed
Outside try-catch-finally clause

2、try-block发生的异常在catch块中没有处理:这种情况下,遵循默认的处理机制。如果 finally 块存在,它将按照默认处理机制执行。

  • try-catch 子句:

Java

// Java program to demonstrate
// control flow of try-catch clause
// when exception occurs in try block
// but not handled in catch block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        // not a appropriate handler
        catch(NullPointerException ex)
        {
            System.out.println("Exception has been caught");
        }
         
        // rest program will not execute
        System.out.println("Outside try-catch clause");
    }
}

运行时错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at GFG.main(GFG.java:12)
  • try-catch-finally 子句:

Java

// Java program to demonstrate
// control flow of try-catch-finally clause
// when exception occur in try block
// but not handled in catch block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
         
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        // not a appropriate handler
        catch(NullPointerException ex)
        {
            System.out.println("Exception has been caught");
        }
         
        finally
        {
            System.out.println("finally block executed");
        }
         
        // rest program will not execute
        System.out.println("Outside try-catch-finally clause");
    }
}

输出 :

finally block executed

运行时错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at GFG.main(GFG.java:12)

3. try-block 中不会发生异常:在这种情况下,catch 块永远不会运行,因为它们只在发生异常时运行。 finally 块(如果存在)将被执行,然后是程序的其余部分。

  • try-catch 子句:

Java

// Java program to demonstrate try-catch
// when an exception doesn't occurred in try block
class GFG
{
    public static void main (String[] args)
    {
         
        try
        {
             
            String str = "123";
                 
            int num = Integer.parseInt(str);
                 
            // this statement will execute
            // as no any exception is raised by above statement
            System.out.println("Inside try block");
             
        }
         
        catch(NumberFormatException ex)
        {
                 
            System.out.println("catch block executed...");
                 
        }
 
        System.out.println("Outside try-catch clause");
    }
}

输出 :

Inside try block
Outside try-catch clause
  • try-catch-finally 子句

Java

// Java program to demonstrate try-catch-finally
// when exception doesn't occurred in try block
class GFG
{
    public static void main (String[] args)
    {
         
    try
    {
         
        String str = "123";
             
        int num = Integer.parseInt(str);
             
        // this statement will execute
        // as no any exception is raised by above statement
        System.out.println("try block fully executed");
         
    }
     
    catch(NumberFormatException ex)
    {
             
        System.out.println("catch block executed...");
             
    }
     
    finally
    {
        System.out.println("finally block executed");
    }
 
    System.out.println("Outside try-catch-finally clause");
    }
}

输出 :

try block fully executed
finally block executed
Outside try-catch clause

try-finally 中的控制流

在这种情况下,无论try-block是否发生异常, finally都会被执行。但控制流将取决于 try 块中是否发生异常。

1.异常引发:如果try块发生异常,则控制流将被finally块遵循默认的异常处理机制。

Java

// Java program to demonstrate
// control flow of try-finally clause
// when exception occur in try block
class GFG
{
    public static void main (String[] args)
    {
         
        // array of size 4.
        int[] arr = new int[4];
        try
        {
            int i = arr[4];
                 
            // this statement will never execute
            // as exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        finally
        {
            System.out.println("finally block executed");
        }
         
        // rest program will not execute
        System.out.println("Outside try-finally clause");
    }
}

输出 :

finally block executed
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at GFG.main(GFG.java:11)

2. 未引发异常:如果 try 块中没有发生异常,则控制流将在 finally 块之后,然后是程序的其余部分

Java

// Java program to demonstrate
// control flow of try-finally clause
// when exception doesn't occur in try block
class GFG
{
    public static void main (String[] args)
    {
         
        try
        {
            String str = "123";
             
            int num = Integer.parseInt(str);
             
            // this statement will execute
            // as no any exception is raised by above statement
            System.out.println("Inside try block");
        }
         
        finally
        {
            System.out.println("finally block executed");
        }
         
        // rest program will be executed
        System.out.println("Outside try-finally clause");
    }
}

输出 :

Inside try block
finally block executed
Outside try-finally clause

相关文章:

  • 在Java中抛出并抛出
  • Java中的异常类型
  • Java中的已检查异常与未检查异常