1. Higher layers should catch lower-level exceptions and, in their place, throw exceptions that can be explained in terms of the higher-level abstraction. This idiom is known as exception translation.
2. A special form of exception translation called exception chaining is appropriate in cases where the lower-level exception might be helpful to someone debugging the problem that caused the higher-level exception. The lower-level exception (the cause) is passed to the higher-level exception, which provides an accessor method (Throwable.getCause) to retrieve the lower-level exception. Most standard exceptions have chaining-aware constructors. For exceptions that don’t, you can set the cause using Throwable’s initCause method. Not only does exception chaining let you access the cause programmatically (with getCause), but it integrates the cause’s stack trace into that of the higher-level exception.
3. Where possible, the best way to deal with exceptions from lower layers is to avoid them, by ensuring that lower-level methods succeed. Sometimes you can do this by checking the validity of the higher-level method’s parameters before passing them on to lower layers. If it is impossible to prevent exceptions from lower layers, the next best thing is to have the higher layer silently work around these exceptions, insulating the caller of the higher-level method from lower-level problems. Under these circumstances, it may be appropriate to log the exception using some appropriate logging facility such as java.util.logging. This allows an administrator to investigate the problem, while insulating the client code and the end user from it.
相关推荐
Item 73: Throw exceptions appropriate to the abstraction Item 74: Document all exceptions thrown by each method Item 75: Include failure-capture information in detail messages Item 76: Strive for ...
**Item 3: Prefer the `is` or `as` Operators to Casts** - **Advantages:** These operators provide type checking and casting without the need for exception handling. - **Example:** ```csharp object ...
4. **throw与throwing**:`throw`语句不仅用于抛出异常,还可以在`catch`块内部使用,重新抛出异常或抛出新的异常,以改变错误处理的路径。 5. **自定义错误类型**:通过继承Error对象,可以创建自定义错误类型,这...
SMTP server will relay the message on to the SMTP server of the recipient(s) to eventually be acquired by the user(s) through POP or IMAP. This does not require your SMTP server to be an open relay,...
Exceptions to the Rules Existing Non-conformant Code Windows Code Important Note Displaying Hidden Details in this Guide link ▶This style guide contains many details that are initially hidden from ...
- **noexcept:** Use noexcept specifier to indicate that a function does not throw exceptions. - **RTTI:** Use RTTI (Run-Time Type Information) judiciously as it can impact performance. - **Casting:** ...
- `point to`: 指向方向或位置,例如:He pointed to the north. - `prefer to do...`: 更喜欢做某事,如:I prefer to read books rather than watch TV. - `regard ... as ...`: 把某物视为,如:We regard him as...
throw er; // Unhandled \'error\' event ^ Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE at SecurePair. (tls.js:1381:32) at SecurePair.emit (events.js:92:17) at SecurePair.maybeInitFinished (tls.js:980:10) at ...
throws與throw的區別 在Java编程中,throws和throw是兩個與异常处理相關的关键字,虽然它们都是用于处理异常的,但它们的使用场景和语法不同。下面我们将详细解释throws和throw的區別。 throws throws是方法抛出...
### C#中的throw语句详解 #### 一、throw语句概述 在C#编程语言中,`throw`语句是一种重要的异常处理机制的一部分。它主要用于在程序运行时发生异常情况时,显式地抛出一个异常对象。`throw`语句常常与`try`、`...
- 在公园里到处乱扔垃圾:throw rubbish everywhere in the park - 这个问题很容易,他能回答出来:The question is easy enough for him to answer. - 别乱扔垃圾,保持教室干净:Don’t litter. Keep the ...
`Laravel开发-exceptions`着重关注的就是这一核心功能。异常处理不仅有助于调试,还能提升用户体验,因为它可以定制化错误页面,避免暴露敏感信息。下面将详细探讨Laravel中的异常处理机制。 1. **异常处理基础** ...
This in turn results in frustration when the abstraction that jQuery provides "leaks" and exposes you to the native aspects of the browser. This book aims to educate you and help you solve common ...
There are some ViewGroups (ones that utilize onInterceptTouchEvent) that throw exceptions when a PhotoView is placed within them, most notably ViewPager and DrawerLayout. This is a framework issue ...
在给定的场景中,`bash-loop-throw-exceptions` 可能是指一个Bash脚本,该脚本设计了一个循环来处理多个jar文件的执行。这样的脚本可能用于测试、批处理操作或者持续集成流程,其中需要多次运行同一个Java应用程序...
- **到处乱扔包装袋**:throw the wrappers everywhere - **污染校园**:pollute the campus - **有规律的一日三餐**:three regular meals a day - **均衡饮食**:a balanced diet - **拒绝吃零食**:say no ...