Scala Exception

Scala的异常通过try catch finally来处理,类似java,但是有诸多不同:

try {
  remoteCalculatorService.add(1, 2)
} catch {
  case e: ServerIsDownException => log.error(e, "the remote calculator service is unavailable. should have kept your trusty HP.")
} finally {
  remoteCalculatorService.close()
}

不像Java中使用多个catch语句,Scala用一个catch语句捕获所有异常,然后根据模式匹配决定异常如何处理,逻辑上与Java的多个catch是一样的。

很重要的一点,try整个也是作为表达式( expression-oriented),可以将整个try赋给其他变量。

val result: Int = try {
  remoteCalculatorService.add(1, 2)
} catch {
  case e: ServerIsDownException => {
    log.error(e, "the remote calculator service is unavailable. should have kept your trusty HP.")
    0
  }
} finally {
  remoteCalculatorService.close()
}

这样就不需要像Java现在try外面生命变量,然后在try或catch中给变量赋值。需要注意的是:finally块会在异常被处理(如果有)后执行,但是finally不作为表达式的一部分(因此没有返回值)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Scala is a modern programming language for the Java Virtual Machine (JVM) that combines the best features of object-oriented and functional programming languages. Using Scala, you can write programs more concisely than in Java, as well as leverage the full power of concurrency. Since Scala runs on the JVM, it can access any Java library and is interoperable with Java frameworks. Scala for the Impatient concisely shows developers what Scala can do and how to do it. In this book, Cay Horstmann, the principal author of the international best-selling Core Java(t), offers a rapid, code-based introduction that's completely practical. Horstmann introduces Scala concepts and techniques in 'blog-sized' chunks that you can quickly master and apply. Hands-on activities guide you through well-defined stages of competency, from basic to expert. Coverage includes * Getting started quickly with Scala's interpreter, syntax, tools, and unique idioms * Mastering core language features: functions, arrays, maps, tuples, packages, imports, exception handling, and more * Becoming familiar with object-oriented programming in Scala: classes, inheritance, and traits * Using Scala for real-world programming tasks: working with files, regular expressions, and XML * Working with higher-order functions and the powerful Scala collections library * Leveraging Scala's powerful pattern matching and case classes * Creating concurrent programs with Scala actors * Implementing domain-specific languages * Understanding the Scala type system * Applying advanced 'power tools' such as annotations, implicits, and delimited continuations Scala is rapidly reaching a tipping point that will reshape the experience of programming. This book will help object-oriented programmers build on their existing skills, allowing them to immediately construct useful applications as they gradually master advanced programming techniques.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值