JavaScript Error Handling

wahyu eko hadi saputro
3 min readMay 8, 2022

Every programming language must be having exception/error handling style. Proper error handling is needed to create a robust application. Error itself is an anomaly, for example: the program expects numeric value but gives string data. As a programmer, we need to decide when an error happens, whether catch the error or throw the error. For more understanding let’s deep dive :

  1. Java script throw statement

In java script, we can throws many expression, but the best to throw an instance of Error or its subclasses.

throw 'Error2';   // String typethrow 42;         // Number typethrow true;       // Boolean typethrow {toString: function() { return "I'm an object!"; } };throw new Error('Problem!'); // throws object error

Error class :

Error is the common superclass of all built-in error classes (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error),

It has the following sub-Classes:
RangeError : Indicates a value that is not in the set or range of allowable values.
ReferenceError : Indicate that an invalid reference value has been detected.
SyntaxError : Indicates that a parsing error has occurred.
TypeError : is used to indicate an unsuccessful operation when none of the other
NativeError objects are an appropriate indication of the failure cause.
URIError : Indicates that one of the global URI handling functions was used in a way that is incompatible with its definition.
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError/RangeError
)

Example the use of Error class :

const err = new Error('Could not find the file');
console.log(Error.prototype.message)
Error class properties and method
the use of error class

Custom error class

Based on my experience by reading another style of error handling code, it is recommended to create custom class for error handling. As we know there 2 types of error, first one handled error and unhandled error. Handled error means programmer know where is the code possibly having bugs, example : expected input is integer but given decimal or string as the input. Unhandled error means a programmer does not have 100% visibility where the error come from, example error that thrown by third party library or runtime environment library.

class CustomException extends Error{
constructor(errorCode, customMessage) {
super(customMessage)
this.errorCode = errorCode;
this.customMessage = customMessage;
}
}
const customExcep = new CustomException("001", "field must be number");
CustomerException class properties and method
The use of CustomError class

2. Try-cacth-finally

Try : put code having potential error on try statement
Catch : catch the error that thrown by try statement
Finally : finally statement is always executed whether error or not

We can combine try-catch-finally as follows:

• try-catch

• try-finally

  • try-catch-finally
Syntax of try-catch:try {«try_statements»} catch (error) {«catch_statements»}Syntax of try-finally:try {«try_statements»} finally {«finally_statements»}Syntax of try catch finally:try {«try_statements»} catch (error) {«catch_statements»} finally {«finally_statements»}

cuatome class

class CustomException extends Error{
constructor(errorCode, customMessage) {
super(customMessage)
this.errorCode = errorCode;
this.customMessage = customMessage;
}
}
try{
throw new CustomException("001", "field must be number");
}
catch(e){
if (e instanceof CustomException){
console.log(e.customMessage);
}
}finally{
console.log("finally always executed");
}
example of use CustomException class

Source :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

Book : JavaScript for impatient programmers (ES2021 edition)

--

--