Error Handling
Part of software development involves the handling of errors. The more complex a system is, the more chances that something will go wrong. If possible, it's always better to anticipate a problem and mitigate it than to have if fall to the default handling of the error, which is not useful to the user.
Benefits of Exception Handling
- Graceful Degradation: If the error occurs, then also the program keeps on running smoothly.
- Error Logging: It helps by recording error details, making it easier to find and fix problems.
- Prevents Crashes: It ensures the app doesn't completely stop working when something goes wrong.
Best Practices for Error Handling
- Catch Specific Errors: Handle specific error types if possible.
- Use finally for Cleanup: Ensure resources are released or closed.
- Avoid Silent Failures: Always log or handle errors appropriately.
- Use Custom Errors: Provide meaningful error messages.
Error Object
Error objects are thrown when runtime errors occur.
The Error object can also be used as a base object for user-defined exceptions.
Runtime errors result in new Error objects being created and thrown.
Error() can be called with or without new. Both create a new Error instance.
Error Types
Besides the generic Error constructor, there are other core error constructors in JavaScript.
Type | Description |
---|---|
EvalError | Creates an instance representing an error that occurs regarding the global function eval(). |
RangeError | Creates an instance representing an error that occurs when a numeric variable or parameter is outside its valid range. |
ReferenceError | Creates an instance representing an error that occurs when de-referencing an invalid reference. |
SyntaxError | Creates an instance representing a syntax error. |
TypeError | Creates an instance representing an error that occurs when a variable or parameter is not of a valid type. |
URIError | Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters. |
AggregateError | Creates an instance representing several errors wrapped in a single error when multiple errors need to be reported by an operation, for example by Promise.any(). |
Static Methods
- Error.isError(): Returns true if the argument is an error, or false otherwise.
Instance Properties
These properties are defined on Error.prototype and shared by all Error instances.
- Error.prototype.constructor: The constructor function that created the instance object. For Error instances, the initial value is the Error constructor.
- Error.prototype.name: Represents the name for the type of error. For Error.prototype.name, the initial value is "Error". Subclasses like TypeError and SyntaxError provide their own name properties.
- cause: Error cause indicating the reason why the current error is thrown — usually another caught error. For user-created Error objects, this is the value provided as the cause property of the constructor's second argument.
- message: Error message. For user-created Error objects, this is the string provided as the constructor's first argument.