REST (Jersey Implementation) Unified exception mechanism

Source: Internet
Author: User
Tags try catch

One: Exception classification

(1) The Throwable class is the ancestor of all exceptions, and it has two direct sub-classes Error/exception:
Error throws an Error object only when a dynamic connection failure occurs in a Java virtual machine or if another location fails. The general program does not need to catch or throw an Error object.


(2) Unchecked Exception:
A. Refers to a program's flaws or logic errors, and cannot be recovered at run time.
B. Includes error and RuntimeException and its subclasses, such as: OutOfMemoryError, Undeclaredthrowableexception, IllegalArgumentException, Illegalmonitorstateexception, NullPointerException, IllegalStateException, indexoutofboundsexception and so on.
C. There is no need to declare thrown exceptions in syntax.


(3) Checked Exception:
A. Invalid external conditions (e.g. user input, database problems, network anomalies, file loss, etc.) not directly controlled by the representative program
B. In addition to error and RuntimeException and its subclasses, such as: ClassNotFoundException, Namingexception, Servletexception, SQLException, IOException and so on.
C. A try catch processing or throws declaration is required to throw an exception.


Second: Exception handling principle

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

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

3, through the first two points of view, the system in the custom exception will only exist unchecked exception, the system is only the client Exchange data on the upper level, set up a unified exception handling mechanism, and some of the anomalies into the user can understand the information communicated to the user.

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


Three: Jersey exception handling mechanism example


(1) Create Exceptionmappersupport Unified Exception processor under the package of API




(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 Tips




(6): API call, exception parsing



(7): Abnormal prompt effect



REST (Jersey Implementation) uniform exception mechanism

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.