- Scala Programming Projects
- Mikael Valot Nicolas Jorand
- 231字
- 2021-07-23 16:25:24
Using exceptions
Exceptions are one of the mechanisms that we can use in Scala to handle error scenarios. It consists of two statements:
- The throw exceptionObject statement stops the current function and passes the exception up to the caller.
- The try { myFunc() } catch { case pattern1 => recoverExpr1 } statement catches any exception thrown by myFunc() if the exception matches one of the patterns inside the catch block:
- If an exception is thrown by myFunc, but no pattern matches the exception, the function stops, and the exception is passed up to the caller again. If there is no try...catch block in the call chain that can catch the exception, the whole program stops.
- If an exception is thrown by myFunc, and the pattern1 pattern matches the exception, the try...catch block will return the recoverExpr1 expression at the right of the arrow.
- If no exception is thrown, the try...catch block returns the result returned by myFunc().
This mechanism comes from Java, and since the Scala SDK sits on top of the Java SDK, many function calls to the SDK can throw exceptions. If you are familiar with Java, the Scala exception mechanism differs slightly. Exceptions in Scala are always unchecked, which means that the compiler will never force you to catch an exception or declare that a function can throw an exception.
推薦閱讀
- 微商之道
- 智慧城市中的移動互聯(lián)網(wǎng)技術(shù)
- PLC、現(xiàn)場總線及工業(yè)網(wǎng)絡(luò)實用技術(shù)速成
- 計算機網(wǎng)絡(luò)與通信(第2版)
- 計算機網(wǎng)絡(luò)原理與應(yīng)用技術(shù)
- 雷達(dá)饋線技術(shù)
- 圖解手機元器件維修技巧
- 網(wǎng)管員必讀:網(wǎng)絡(luò)管理(第2版)
- 高級網(wǎng)絡(luò)技術(shù)
- 設(shè)備監(jiān)控技術(shù)詳解
- 一本書讀懂TCP/IP
- 云工廠:開啟中國制造云時代
- 精通SEO:100%網(wǎng)站流量提升密碼
- 物聯(lián)網(wǎng)
- 物聯(lián)網(wǎng)概論