A collection of custom exception classes written in JavaScript.
Error
└── Exception
├── ArgumentException
├── InvalidInputException
├── NotImplementedException
├── NotSupportedException
├── NotFoundException
├── UnauthenticatedException
└── UnauthorizedException
Class Name | Description | Default Message |
---|---|---|
Exception | The exception that is thrown when an error occurs. | No Description |
ArgumentException | The exception that is thrown when one of the arguments provided to a method is not valid. | Invalid Argument |
InvalidInputException | The exception that is thrown when one of the input provided to a method is not valid. | Invalid Input |
NotImplementedException | The exception that is thrown when a requested method or operation is not implemented. | Not Implemented |
NotSupportedException | The exception that is thrown when a requested method or operation is not supported. | Not Supported |
NotFoundException | The exception that is thrown when an attempt is made to find something that does not exist. | Not Found |
UnauthenticatedException | The exception that is thrown when a requested method or operation requires authentication. | Not Authenticated |
UnauthorizedException | The exception that is thrown when the current user is not allowed to perform an attempted operation. | Not Authorized |
Initiate any one of the exception classes.
if (item == null) {
throw new NotFoundException("Item not found.");
}
Check the type of exception we are dealing with:
var err = new Exception();
err instanceof Exception // → true
err instanceof Error // → true
Exception.prototype.isPrototypeOf(err) // → true
Error.prototype.isPrototypeOf(err) // → true
err.constructor.name // → "Exception"
err.name // → "Exception"
err.message // → "No description"
err.toString() // → "[Exception] No description"
err.stack // → prints the stack trace
Handle specific exception types using exception's constructor property:
try {
foo("valA", "valB");
}
catch (e) {
if (e instanceof ArgumentException) {
console.error(e.toString());
}
else if (e instanceof InvalidInputException) {
console.error(e.toString());
}
else {
console.error(e.toString());
}
}
Exception.io is released under the MIT license. See LICENSE for details.