Web Kokua Banner Image

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

Best Practices for Error Handling

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.

new Error() new Error(message) new Error(message, options) new Error(message, fileName) new Error(message, fileName, lineNumber) Error() Error(message) Error(message, options) Error(message, fileName) Error(message, fileName, lineNumber)

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.

ECMAScript Exception
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

Instance Properties

These properties are defined on Error.prototype and shared by all Error instances.

The Try Catch Block

try { // ATTEMPT TO DO SOMETHING } catch (e) { // IF IT FAILS, HANDLE THE FAILURE } finally { // Optional: Code that runs regardless of success or error }

Targeting Specific Errors

try { some_function(); } catch (e) { if (e instanceof EvalError) { console.error(`${e.name}: ${e.message}`); } else if (e instanceof RangeError) { console.error(`${e.name}: ${e.message}`); } // etc. else { // If none of our cases matched leave the Error unhandled throw e; } }
function check_error_01(result_name, n1, n2, dp) { let divResult = document.getElementById(result_name); let errMsg = ""; let result = ""; try { result = divide(n1, n2, dp); } catch (ex) { if (ex instanceof EvalError) { errMsg = "Eval Error<br/>name: "+ ex.name +"<br/>message:"+ ex.message; } else if (ex instanceof RangeError) { errMsg = "Range Error<br/>name: " + ex.name + "<br/>message:" + ex.message; } else { errMsg = ex.message; } } if (errMsg != "") { divResult.innerHTML = "Error: " + errMsg; } else { divResult.innerHTML = "Result: " + result; } } function divide(n1, n2, dp) { return (n1 / n2).toFixed(dp); }
Divide(2,3,4) Divide(3,2,0) Divide(2,3,-1) Divide(2,3,2)

Try Catch Response


function check_error_02(result_name) { let divResult = document.getElementById(result_name); let errMsg = ""; try { throw new Error("Some Error Occured"); } catch (ex) { divResult.innerHTML = "name: " + ex.name +"<br/>message: "+ ex.message; } }
Custom Error

Response