`

Handling Errors Using Exceptions

阅读更多
Everyone tries to avoid them, but it's an unfortunate fact: Errors occur in software programs. However, if you handle errors properly, you'll greatly improve programs' readability, reliability and maintainability. The Java programming language uses exceptions for error handling.
What Is an Exception?
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.

The Catch or Specify Requirement
There are two kinds of exceptions: checked exceptions and un-checked exceptions.
it is the bottom line guildline:If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

Best Practices for Designing the API
1.When deciding on checked exceptions vs. unchecked exceptions, ask yourself, "What action can the client code take when the exception occurs?"
Moreover, prefer unchecked exceptions for all programming errors: unchecked exceptions have the benefit of not forcing the client API to explicitly deal with them. They propagate to where you want to catch them, or they go all the way out and get reported.

2.Preserve encapsulation.
Never let implementation-specific checked exceptions escalate to the higher layers. For example, do not propagate SQLException from data access code to the business objects layer. Business objects layer do not need to know about SQLException. You have two options:

>Convert SQLException into another checked exception, if the client code is expected to recuperate from the exception.
>Convert SQLException into an unchecked exception, if the client code cannot do anything about it.
a recommended way is:
public void dataAccessCode(){
    try{
        ..some code that throws SQLException
    }catch(SQLException ex){
        throw new RuntimeException(ex);
    }
not:
public void dataAccessCode(){
    try{
        ..some code that throws SQLException
    }catch(SQLException ex){
        ex.printStacktrace();
    }
}
for the last one, client can do nothing, so we can throw it to upper tier to let him/her know what had happened.

3. how to throw exceptions?
    for those checked exceptions, such as Integer.parseInt("ABC"), I/O exception. we can catch it on the business level, deal it with a default manner or throw it to the upper tier(by convert it to a recognised exception) which have the ability to deal with it.
    for those unchecked exceptions, throw it to upper tier to deal with.
分享到:
评论

相关推荐

    a_byte_of_python

    Handling Exceptions Raising Exceptions How To Raise Exceptions Try..Finally Using Finally Summary 14. The Python Standard Library Introduction The sys module Command Line Arguments More sys ...

    Microsoft Visual C# 2010 Step by Step Mar 2010

    6 Managing Errors and Exceptions 109 Part II Understanding the C# Language 7 Creating and Managing Classes and Objects 129 8 Understanding Values and References 151 9 Creating Value Types with ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Tips for Using Exceptions 568 Using Assertions 571 Logging 575 Debugging Tips 591 Using a Debugger 607 Chapter 12: Generic Programming 613 Why Generic Programming? 614 Definition of a Simple...

    Python Tutorial 入门指南3.6英文版

    8. Errors and Exceptions 81 8.1. Syntax Errors 81 8.2. Exceptions 81 8.3. Handling Exceptions 82 8.4. Raising Exceptions 86 8.5. User-defined Exceptions 87 8.6. Defining Clean-up Actions 88 8.7. ...

    [Mastering.Node.js(2013.11) 精通Node.js

    Errors and exceptions 272 The domain module 275 Headless website testing with ZombieJS and Mocha 277 Mocha 278 Headless web testing 279 Using Grunt, Mocha, and PhantomJS to test and deploy projects ...

    Lean.Python.Learn.Just.Enough.Python.to.Build.Useful.Tools

    Handle errors and exceptions Test your code Access the Web; do searching; and persist data Who This Book Is For This book is aimed at three categories of reader: The experienced programmer – if you ...

    Joomla! 1.5 Development Cookbook.pdf

    Logging errors and events using JLog 295 Throwing exceptions with PHP 5 298 Catching exceptions with PHP 5 300 This material is copyright and is licensed for the sole use by Vadim Kudria on 4th ...

    Professional C# 3rd Edition

    Error Handling with Exceptions 16 Use of Attributes 17 Assemblies 17 Private Assemblies 18 Shared Assemblies 19 Reflection 19 .NET Framework Classes 19 Namespaces 21 Creating .NET Applications Using ...

    C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition

    Chapter 3, Controlling Flow, Converting Types, and Handling Exceptions, is about writing code that makes decisions, repeats a block of statements, converts between types, and handles errors. You will ...

    Beginning Python (2005).pdf

    Handling Errors 272 Summary 273 Exercises 274 Chapter 15: Using Python for XML 275 What Is XML? 275 A Hierarchical Markup Language 275 A Family of Standards 277 What Is a Schema/DTD? 278 What ...

    Microsoft Visual C# 2013 Step by Step,最新资料

    CHAPTER 6 Managing errors and exceptions 135 PART II UNDERSTANDING THE C# OBJECT MODEL CHAPTER 7 Creating and managing classes and objects 161 ChAPTeR 8 Understanding values and references 183 CHAPTER...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    7.3. Handling exceptions 7.3.1. Mapping exceptions to HTTP status codes 7.3.2. Writing exception-handling methods 7.4. Advising controllers 7.5. Carrying data across redirect requests 7.5.1. ...

    java.核心技术.第八版 卷1

    Tips for Using Exceptions 568 Using Assertions 571 Logging 575 Debugging Tips 591 Using a Debugger 607 Chapter 12: Generic Programming 613 Why Generic Programming? 614 Definition of a Simple ...

    Thinking in Java 4th Edition

    Exception handling: dealing with errors ............... 31 Concurrent programming ... 32 Java and the Internet .......... 33 What is the Web? ......................... 33 Client-side programming ........

    python3.6.5参考手册 chm

    Changes To Exceptions Miscellaneous Other Changes Operators And Special Methods Builtins Build and C API Changes Performance Porting To Python 3.0 What’s New in Python 2.7 The Future for Python...

    EurekaLog_7.5.0.0_Enterprise

    18)..Fixed: Possible "Unit XYZ was compiled with a different version of ABC" when using packages 19)..Fixed: FastMM shared MM compatibility 20)..Fixed: Minor bugs in stack tracing (which usually ...

    Program in LUA 2nd Edition.rar

    8.4 Error Handling and Exceptions 69 8.5 Error Messages and Tracebacks 70 9 Coroutines 73 9.1 Coroutine Basics 73 9.2 Pipes and Filters 76 9.3 Coroutines as Iterators 79 9.4 Non-Preemptive ...

    ZendFramework中文文档

    14.5.4. Using Metacommands to Control Filter or Validator Rules 14.5.4.1. The FIELDS metacommand 14.5.4.2. The PRESENCE metacommand 14.5.4.3. The DEFAULT_VALUE metacommand 14.5.4.4. The ALLOW_...

    串口通讯控件

    - Added defaulting mechanism for handshake thresholds to avoid errors due to fixed defaults conflicting with driver queue size defaults. - Changed default for sendTimeoutMultiplier for not NT-...

Global site tag (gtag.js) - Google Analytics