Scala教程
Scala面向对象
Scala高级

Scala Throw关键字

Scala Throw 关键字

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

Scala Throw Example

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)
 
    }
}
输出:
java.lang.ArithmeticException: You are not eligible
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4