Scala Throw关键字用法示例

你可以在代码中明确抛出异常。 Scala提供throw关键字引发异常。 throw关键字主要用于引发自定义异常。下面给出一个使用scala throw exception关键字的示例。

Scala投掷示例

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
微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_47722.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?