Spark记录-Scala异常与处理

Scala try-catch语句

Scala提供trycatch块来处理异常。try块用于包含可疑代码。catch块用于处理try块中发生的异常。可以根据需要在程序中有任意数量的try...catch块。

Scala try catch示例1

在下面的程序中,我们将可疑代码封装在try块中。 在try块之后使用了一个catch处理程序来捕获异常。如果发生任何异常,catch处理程序将处理它,程序将不会异常终止。

class ExceptionExample{  
    def divide(a:Int, b:Int) = {  
        try{  
            a/b  
        }catch{  
            case e: ArithmeticException => println(e)  
        }  
        println("Rest of the code is executing...")  
    }  
}  
object Demo{  
    def main(args:Array[String]){  
        var e = new ExceptionExample()  
        e.divide(100,0)  

    }  
}
Scala

将上面代码保存到源文件:Demo.scala中,使用以下命令编译并执行代码 -

D:softwarescala-2.12.3in>scalac Demo.scala
D:softwarescala-2.12.3in>scala Demo.scal
java.lang.ArithmeticException: / by zero
Rest of the code is executing...
Shell

Scala Try Catch示例2

在这个例子中,catch处理程序有两种情况。 第一种情况将只处理算术类型异常。 第二种情况有Throwable类,它是异常层次结构中的超类。第二种情况可以处理任何类型的异常在程序代码中。有时当不知道异常的类型时,可以使用超类 - Throwable类。

class ExceptionExample{  
    def divide(a:Int, b:Int) = {  
        try{  
            a/b  
            var arr = Array(1,2)  
            arr(10)  
        }catch{  
            case e: ArithmeticException => println(e)  
            case ex: Throwable =>println("found a unknown exception"+ ex)  
        }  
        println("Rest of the code is executing...")  
    }  
}  
object Demo{  
    def main(args:Array[String]){  
        var e = new ExceptionExample()  
        e.divide(100,10)  

    }  
}
Scala

将上面代码保存到源文件:Demo.scala中,使用以下命令编译并执行代码 -

D:softwarescala-2.12.3in>scalac Demo.scala
D:softwarescala-2.12.3in>scala Demo.scal
found a unknown exceptionjava.lang.ArrayIndexOutOfBoundsException: 10
Rest of the code is executing...

Scala finally块

inally块用于在异常时释放资源。资源可能是文件,网络连接,数据库连接等,finally块执行代码运行保证。以下程序说明了finally块的用法。

Scala finally块示例

class ExceptionExample{  
    def divide(a:Int, b:Int) = {  
        try{  
            a/b  
            var arr = Array(1,2)  
            arr(10)  
        }catch{  
            case e: ArithmeticException => println(e)  
            case ex: Exception =>println(ex)  
            case th: Throwable=>println("found a unknown exception"+th)  
        }  
        finally{  
            println("Finaly block always executes")  
        }  
        println("Rest of the code is executing...")  
    }  
}  


object Demo{  
    def main(args:Array[String]){  
        var e = new ExceptionExample()  
        e.divide(100,10)  

    }  
}
Scala

将上面代码保存到源文件:Demo.scala中,使用以下命令编译并执行代码 -

D:softwarescala-2.12.3in>scalac Demo.scala
D:softwarescala-2.12.3in>scala Demo.scal
java.lang.ArrayIndexOutOfBoundsException: 10
Finally block always executes
Rest of the code is executing...

Scala throw关键字

可以在代码中明确地抛出异常。Scala提供throw关键字来抛出异常。 throw关键字主要用于抛出自定义异常。下面给出了使用scala throw异常关键字的例子。

Scala Throw示例

class ExceptionExample2{  
    def validate(age:Int)={  
        if(age<18)  
            throw new ArithmeticException("You are not eligible")  
        else println("You are eligible")  
    }  
}  

object MainObject{  
    def main(args:Array[String]){  
        var e = new ExceptionExample2()  
        e.validate(10)  

    }  
}

Scala throws关键字

Scala提供了throws关键字来声明异常。可以使用方法定义声明异常。 它向调用者函数提供了此方法可能引发此异常的信息。 它有助于调用函数处理并将该代码包含在try-catch块中,以避免程序异常终止。在scala中,可以使用throws关键字或throws注释来声明异常。

Scala Throws示例

class ExceptionExample4{  
    @throws(classOf[NumberFormatException])  
    def validate()={  
        "abc".toInt  
    }  
}  

object Demo{  
    def main(args:Array[String]){  
        var e = new ExceptionExample4()  
        try{  
            e.validate()  
        }catch{  
            case ex : NumberFormatException => println("Exception handeled here")  
        }  
        println("Rest of the code executing...")  
    }  
}

 
原文地址:https://www.cnblogs.com/xinfang520/p/7728869.html