`
leonzhx
  • 浏览: 767922 次
  • 性别: 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 ...

    C#_7 Quick Syntax Reference, 2nd Edition--2018

    A Pocket Guide to the Language, APIs, and Library What You Will Learn Discover what’s new in C# 7.3 and .NET for Windows 10 programming, including out values, ref locals and returns, local ...

    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开源基础库abseil-cpp

    它是从Google内部代码块中抽取出来的一系列最基础的软件库。作为基本的组成部分,这些软件库支撑了几乎全部Google在运行的项目。以前这些API是零零散散地嵌入在Google的大部分开源项目中,现在我们将它们规整在一起...

    Beyond Bullet Points Using PowerPoint to tell a compelling story

    “Throw away those room-emptying, left-brain slides–and use Atkinson’s book to turn your PowerPoint presentation into an epic.” –Daniel H. Pink, author of Drive and A Whole New Mind Think ...

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

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

    C# throw詳解

    throw异常:C#语言throw异常处理语句, throw语句用于发出在执行期间出现反常情况(异常)信号throw语句通常和try-catch或try-finally语句起使用可以使用throw语句显式引发异常(这里引发自定义异常)创建用户自定义异常...

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

    Beyond jQuery(Apress,2016)

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

    Implementing.Cloud.Design.Patterns.for.AWS.1782177345

    In the final leg of your journey, you will get to grips with advanced patterns on Operations and Networking and also get acquainted with Throw-away Environments. Table of Contents Chapter 1: ...

    App Development Recipes for iOS and watchOS 【2016】

    App Development Recipes for iOS and watchOS explores the technical side of app development with tips and tricks to avoid those little things that become big ... outside of the realm of development...

    delphi 2007 第三方补丁

    - Fixed: TTabSheet could throw an access violation if no PageControl was assigned to it 2008-11-24: - Added fix for Show Component Caption IDE bug - Added fix for IDE Compiler opens all files in ...

    JSP Simple Examples

    All methods use the throw statement to throw an exception. The throw statement requires a single argument a throwable object. Here is an example of a throw statement. Arrayindexoutofboundsexception ...

    Implementing.Cloud.Design.Patterns.for.AWS

    In the final leg of your journey, you will get to grips with advanced patterns on Operations and Networking and also get acquainted with Throw-away Environments. Table of Contents Chapter 1: ...

    recursion-200-methods-throw-exceptions

    recursion-200-methods-throw-exceptions 要求 Java ( >=1.6 ) 。 去做: 导入到Eclipse 添加到 vm 参数: -agentlib:TakipiAgent 运行程序。 确保捕获到异常并在堆栈跟踪中显示所有 200 个方法。

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

    Java的throw和return

    Java的throw和return

    The Mythical Man Month(人月神话)20周年英文文字版

    Chapter 11 Plan to Throw One Away 115 Chapter 12 Sharp Tools ,, 127 Chapter 13 The Whole and the Parts 141 Chapter 14 Hatching a Catastrophe , 153 Chapter 15 The Other Face 163 Chapter 16 No Silver ...

Global site tag (gtag.js) - Google Analytics