📜  斯卡拉 |最后例外

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

斯卡拉 |最后例外

Scala finally 块用于执行重要代码,例如关闭连接、流式传输或释放资源(可以是文件、网络连接、数据库连接等)。不管是否抛出异常,它都会被执行。 finally 块将在trycatch块之后执行,但在控制转移回其原点之前。
句法:

try {
       //your scala code here    
    } 
finally {
           println("this block of code is always executed")
           // your scala code here, such as to close a database connection
        }
try-finally 中的控制流

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

  1. 引发异常:如果在 try 块中发生异常,则控制流将被 finally 块遵循默认的异常处理机制。
    例子:
    // Scala program to demonstrate 
    // control flow of try-finally clause 
    // when exception occur in try block 
      
    // Creating object
    object GFG 
    { 
        // Main method 
        def main(args: Array[String]) 
        { 
            var arr = new Array[Int](4) 
              
            try
            { 
                var i = arr(4) 
                  
                // this statement will never execute 
                // as exception is raised by above statement 
                println("Inside try block") 
            } 
              
            finally
            { 
                println("finally block executed") 
            } 
              
            // rest program will not execute 
            println("Outside try-finally clause") 
          
        } 
    }
    

    输出:

    finally block executed
  2. 未引发异常:如果在 try 块中未发生异常,则控制流将在 finally 块之后跟随程序的其余部分。
    例子:
    // Scala program to demonstrate  
    // control flow of try-finally clause 
    // when exception doesn't occur in try block 
      
    // Creating object
    object GFG 
    { 
        // Main method 
        def main(args: Array[String])  
        { 
               
          try
            { 
                val str1 = "123".toInt 
                  
                // this statement will execute 
                // as no any exception is raised 
               // by above statement 
                println("Inside try block") 
            } 
                
            finally
            { 
                println("finally block executed") 
            } 
                
            // rest program will be executed 
            println("Outside try-finally clause") 
        } 
            
      
    }
    

    输出:

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

finally 关键字与 try/catch 块关联使用,并保证将执行一段代码,即使抛出异常也是如此。
例子 :

// Scala program of try-catch-finally clause 
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    { 
        try
        { 
            // create array
            var array = Array(4, 2, 7) 
            var b = array(5) 
        }
        catch
        { 
            // Catch block contain cases.
            case e: ArithmeticException => println(e) 
            case ex: Exception => println(ex) 
            case th: Throwable=> println("unknown exception"+th) 
        } 
        finally
        { 
            // Finally block will execute
            println("this block always executes") 
        } 
          
        // rest program will execute 
        println(" rest of code executing") 
    } 
} 

输出

java.lang.ArrayIndexOutOfBoundsException: 5
this block always executes
Rest of code executing

在上面的示例中,我们在 try 块中创建了一个数组,并从该数组将值分配给变量 b,但它抛出异常,因为我们用于将值分配给变量 b 的数组索引超出了数组索引的范围。 catch 块捕获该异常并打印消息,finally 块无论如何都会执行。