Scala异常处理示例

异常处理是一种用于处理异常情况的机制。你还可以避免程序意外终止。

Scala使”选中与未选中”非常简单。它没有检查的异常。在Scala中, 所有异常均未选中, 甚至SQLException和IOException也未选中。


没有异常处理的Scala程序示例

class ExceptionExample{
    def divide(a:Int, b:Int) = {
            a/b				// Exception occurred here
        println("Rest of the code is executing...")
    }
}
object MainObject{
    def main(args:Array[String]){
        var e = new ExceptionExample()
        e.divide(100, 0)
 
    }
}

输出

java.lang.ArithmeticException: / by zero
微信公众号
手机浏览(小程序)

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_47759.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?