- 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.
推薦閱讀
- FreeSWITCH 1.2
- 網(wǎng)管員典藏書架:網(wǎng)絡(luò)管理與運維實戰(zhàn)寶典
- 工業(yè)控制網(wǎng)絡(luò)安全技術(shù)與實踐
- 物聯(lián)網(wǎng)之魂:物聯(lián)網(wǎng)協(xié)議與物聯(lián)網(wǎng)操作系統(tǒng)
- Proxmox High Availability
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- HTML5 Game development with ImpactJS
- Go Web Scraping Quick Start Guide
- Mastering TypeScript 3
- 物聯(lián)網(wǎng)之霧:基于霧計算的智能硬件快速反應(yīng)與安全控制
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- Bonita Open Solution 5.x Essentials
- 網(wǎng)管工具使用與技巧大全
- 物聯(lián)網(wǎng)頂層設(shè)計與關(guān)鍵技術(shù)
- 互聯(lián)網(wǎng)安全的40個智慧洞見(2016)