REST (Jersey Implementation) unified anomaly mechanism __exception

Source: Internet
Author: User
Tags exception handling

One: Anomaly classification

(1) The Throwable class is the ancestor of all exceptions, and it has two direct subclasses error/exception:
Error throws an Error object only if there is a dynamic connection failure in the Java Virtual machine or when other positioning fails. The general program does not need to catch or throw an Error object.


(2) Unchecked Exception:
A. refers to a flaw in the program or a logic error and cannot be recovered at run time.
B. including error and runtimeexception and their subclasses such as: OutOfMemoryError, Undeclaredthrowableexception, IllegalArgumentException, Illegalmonitorstateexception, NullPointerException, IllegalStateException, indexoutofboundsexception and so on.
C. The syntax does not require a declaration to throw an exception.


(3) Checked Exception:
A. Invalid external situations (such as user input, database problems, network anomalies, file loss, etc.) that are not directly controlled by the program.
B. In addition to the error and runtimeexception and their subclasses, such as: ClassNotFoundException, Namingexception, Servletexception, SQLException, IOException and so on.
C. Requires a try-catch-handling or throws declaration to throw an exception.


Two: the principle of exception handling

1, at the framework level package checked exception, convert it into unchecked exception, avoid the development process of writing cumbersome Try...catch code.

2, the development of the business level, according to the program code responsibility to define different runtimeexception (it is unchecked exception, generally defined as RuntimeException subclass)

3, through the first two views, the system of the custom exception will only exist unchecked exception, the system is only the upper layer of the client Exchange data, set up a unified exception handling mechanism, and some exceptions to the user can understand the message conveyed to the user.

4, other such as business layer, data persistence layer, and so on the bottom is only responsible for the exception can be thrown, but be careful not to lose the exception stack (this is a beginner easy to make a mistake).


Three: Examples of Jersey exception handling mechanism


(1) Create a Exceptionmappersupport unified exception handler under the API package




(2) Custom Exceptions

Exception parent class; persistent layer exception; Business Layer exception



(3) DAO layer exception thrown persistenceexception



(4) Business logic Layer action throws Webactionexceptiontype




(5): Custom Exception friendly hints




(6): API call, exception resolution



(7): Abnormal hint effect



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.