`
leonzhx
  • 浏览: 796792 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 61: Throw exceptions appropriate to the abstraction

阅读更多

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.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    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 ...

    Effective C#

    **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 ...

    Exceptions:探索异常

    4. **throw与throwing**:`throw`语句不仅用于抛出异常,还可以在`catch`块内部使用,重新抛出异常或抛出新的异常,以改变错误处理的路径。 5. **自定义错误类型**:通过继承Error对象,可以创建自定义错误类型,这...

    Java邮件开发Fundamentals of the JavaMail API

    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,...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    Google C++ Style Guide_英文版.pdf

    - **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...

    node.js请求HTTPS报错:UNABLE_TO_VERIFY_LEAF_SIGNATURE\的解决方法

    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的区别

    throws與throw的區別 在Java编程中,throws和throw是兩個與异常处理相關的关键字,虽然它们都是用于处理异常的,但它们的使用场景和语法不同。下面我们将详细解释throws和throw的區別。 throws throws是方法抛出...

    C# throw詳解

    ### C#中的throw语句详解 #### 一、throw语句概述 在C#编程语言中,`throw`语句是一种重要的异常处理机制的一部分。它主要用于在程序运行时发生异常情况时,显式地抛出一个异常对象。`throw`语句常常与`try`、`...

    Java编程技巧:基于throw关键字的学生总成绩异常处理机制

    内容概要:本文介绍了Java中利用throw关键字进行异常处理的一种实践——实现对学生总成绩的计算功能,同时确保传入的成绩数据符合预期的有效范围。文章首先阐述了自定义异常类的设计与作用,继而详细解读了核心计算...

    江苏省靖江市新港城初级中学八年级英语下册 Unit 5 Good manners(第1课时)学案(无答案)(新版)牛津版.doc

    - 在公园里到处乱扔垃圾:throw rubbish everywhere in the park - 这个问题很容易,他能回答出来:The question is easy enough for him to answer. - 别乱扔垃圾,保持教室干净:Don’t litter. Keep the ...

    Laravel开发-exceptions

    `Laravel开发-exceptions`着重关注的就是这一核心功能。异常处理不仅有助于调试,还能提升用户体验,因为它可以定制化错误页面,避免暴露敏感信息。下面将详细探讨Laravel中的异常处理机制。 1. **异常处理基础** ...

    Beyond.jQuery.1484222342

    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 ...

    AndroidPhotoView

    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-loop-throw-exceptions` 可能是指一个Bash脚本,该脚本设计了一个循环来处理多个jar文件的执行。这样的脚本可能用于测试、批处理操作或者持续集成流程,其中需要多次运行同一个Java应用程序...

Global site tag (gtag.js) - Google Analytics