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