`

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.
分享到:
评论

相关推荐

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

    Oracle Database 10g PL-SQL Programming

    - **Exceptions**: Handling errors and exceptions in PL/SQL, including predefined and custom exceptions. #### 4. Using SQL with PL/SQL This chapter explores how to integrate SQL statements within PL/...

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

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

    scipylectures

    - **Exception Handling:** Techniques for handling errors and exceptions gracefully. - **Object-Oriented Programming (OOP):** Principles of OOP, classes, objects, inheritance, and polymorphism. #### ...

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

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

    Addison.Wesley.C++.by.Dissection.2002.pdf

    - **C++ Exceptions:** Discusses exception handling in C++. - **Throwing Exceptions:** Describes throwing exceptions. - **try Blocks:** Explains `try` blocks. - **Handlers:** Discusses exception ...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    **: Exception handling is crucial for managing errors gracefully and ensuring that your application does not crash unexpectedly. - **Exception Handler Syntax**: This section covers the syntax for ...

Global site tag (gtag.js) - Google Analytics