Base Exception Types
The library provides a number of base exception classes that are intended to be used in a specific context. Projects that use the PPWCode libraries typically introduce custom exceptions that derive from these base exceptions, or use the exceptions defined in this library directly. Using these base exception types in the correct context has the benefit that other PPWCode libraries can also be used without requiring customisation.
|
As we will describe further on in the text, using the correct exception (base) types makes it easier to triage thrown exceptions. It also makes it easier to have common code, for example to triage exceptions and translate them into an HTTP status code in an |
The important base exception types are the following:
-
Error-
ProgrammingError -
ExternalError
-
-
SemanticException-
CompoundSemanticException
-
Error
Error is used for signalling undefined behavior of code. This could be due to an internal issue in the code (most likely a bug in the code) or due to an external issue that the code is not designed to handle.
Exceptions that have Error as their base type must never be handled (caught) internally. These exceptions should be handled at the outer boundary of the process or request. At that point the exception should be logged and possibly translated into an error for a calling system.
Note that exceptions of type Error always indicate a problem with the system and the target audience for these errors is the development team (ProgrammingError) or the operations team (ExternalError).
ProgrammingError
ProgrammingError is used when a condition (internal) occurs in the code that we thought impossible when designing the code. This often comes down to an assumption in the code that is broken. We can write defensive code that throws this type of exception when an underlying assumption is violated.
|
Instead of directly throwing |
ExternalError
ExternalError is used when an external condition occurs that we do not want to handle inside the code by design. This can be related to the infrastructure that the application is running on, or to external systems that the application depends on.
A typical example is the unavailability of a REST service required by the application.
SemanticException
SemanticException is used for business exceptions. These are typically used for (user) input that cannot be handled by the application.
Exceptions of type SemanticException typically indicate a problem with the input and the target audience for these exceptions is the user base of the application. Often a translation mechanism must be implemented for these exceptions to present them in a human-readable form to the user.