404 Now Found: Understanding Error Handling in JavaScript

404 Now Found: Understanding Error Handling in JavaScript

Mistakes and failures are an integral part of programming and life in general. Usually, after a mistake in life, we dust ourselves up and try again, but it is hardly so in the world of programming. In JavaScript, when our engine runs into an error when executing our code, it fails and crashes. Before it crashes though, it creates and throws an ‘Exception’.

EXCEPTIONS

An exception is a special javascript object that represents the error encountered. An exception is like a super charged return statement. They come out of the functions where they occur and zoom down the call stack (visit this page to understand more about the call stack), blowing up everything in their way till they get to the end of the stack and eventually crash the program.

Exceptions are not generally bad as they give us feedback which help us write better, error free code. But not all problems can be prevented by the programmer, unfortunately. If your program communicates with the outside world in any way, it is possible to get malformed input, to become overloaded with work, or to have the network fail. We don’t want our program to always fail whenever this occurs, we want it to be able to take the bad input in stride and continue running. Just like in life.

Fortunately JavaScript provides us with a technique to do just that.

TRY/CATCH

If exceptions always zoomed down the stack and blew it up, they wouldn’t be of much use. Try/Catch statements are a way to contain and handle exceptions. It is defined with a try keyword followed by curly braces, and our code is defined inside of it and then a catch statement or a finally statement with curly braces also.

try{
//block of code goes here
}
catch{
//error handing code goes here
}

If an exception occurs inside the try block, they come out of it ( every other thing in the try block will be ignored ) and are handled by code defined in the catch block. After the code in our catch statement runs our engine resumes executing code below the catch block. This way our program doesn’t always crash when there is an error. Errors are handled gracefully in the context they occur in and the rest of the program is executed accordingly.

try{
//block of code goes here
}
catch{
//error handing code goes here
}

console.log(1+1) //this runs despite us encountering an error in the code above

If however no error occurs in the try block, the code in the catch block is ignored and our program execution resumes below the catch block.

FINALLY

The finally keyword specifies code that runs no matter the outcome of the try/catch block. It is defined after them.

try{
//block of code goes here
}
catch{
//error handing code goes here
}
finally{
//runs no matter the outcome of the above
}

ERROR OBJECT

Exceptions are objects that inherit from the global Error object. We can access them in our catch statement by passing exception (e) as an argument to our catch statement.

try{
console.log (doSomething())
}
catch(e){
console.log(e.name, e.message)
}

//output: ReferenceError, doSomething is not defined

The object has three properties

  • E.message : specifies the error message.
  • E.name : specifies the error name.
  • E.stack: specifies the stack of code collapsed.

There are many types of errors, these errors inherit the global Error class, the most common are:

  • TypeError
  • ReferenceError
  • EvalError
  • SyntaxError
  • RangeError

We can also create our own custom exception with the Error() constructor and pass in a string as an argument which gets stored as the error message property. After creating an exception, we can then throw it using the throw keyword. This will be handled like any other error in a try catch block.

const myError = new Error('this is a custom error')

try{
console.log('this is a try block')
throw myError
}
catch(e){
console.log(e.message)
}

//output: 'this is a custom error'

If the throw statement and the defined error is not defined in a try/catch statement, it will break your application like any other error would.