Exceptions
In Angularity, JavaScript runtime errors are divided into two categories: Errors and Exceptions.
- Exceptions are instances of the
class. They are expected to be caught and appropriately handled.Exception - Errors are all the errors that are not instances of
. They are regarded as critical errors that should never happen and typically breaks the application, such asException TypeError
. The occurrence of such an error implies something is wrong within the application that awaits fixes.
To create a type of exception, declare a class that extends from the
class.
class EntityNotFoundException extends Exception {}
Additional customizations can apply to add contextual properties or generate uniformed error messages:
It is worthy to note that both Exceptions and Errors are instances of Error
. The way to distinguish them is instanceof
, not instanceof Error
.
A recommended way to compose the catch block is to exhaust all the exceptions that are expected to be caught and re-throw the error if none of the exceptions are matched:
catch (e) {
if (e instanceof SomeException) {
doSomething();
} else if (e instanceof AnotherException) {
doSomething();
} else {
throw e
}
}
All Angularity libraries follow this convention for error handling, and it is recommended to also follow such convention in applications that uses Angularity.