Error and exception handling in javascript with examples

| 3 years ago

Programming is fun but this sometimes comes up with mistakes and errors.No matter how much of programming experience you have , you always make mistakes and those mistakes result in unexpected results.This is what we term as errors and exceptions.In this article i will talk about errors and exceptions in respect of Javascript,we will see various types of errors with proper examples.So let’s dive into the world of exceptions.

Types of errors in programming

In programming, errors can be any of the following types:
1. Syntax error
2. Runtime error
3. Logical error

Let’s talk about them one by one

Syntax errors

If you don’t know what does syntax means, let me provide a definition to you

Syntax is the arrangement of words and phrases to create well-formed sentences in a language

So, every language have some predefined ways to write a program and when you make some mistake putting words or literals at wrong place and even missing them sometimes, can result into Syntax error.These errors are also refered as Parsing errors


In below example, we have missed the closing parenthesis while using alert()

var str="Hello World";
alert("Hello World";

Runtime errors

These are the errors that occur while compilation or interpretation of your program.Incase of javascript it occurs while the program is interpreted.Whenever a runtime error happens, the code below the line with error will never get executed.


The below code will result into a runtime error at line var result = x.getString();


Uncaught TypeError: x.getString is not a function
at window.onload

Logical errors

These errors are the most difficult to debug.They occur when you make some logical mistake like the business logic or conceputal mistakes.Javascript cannot handle these errors by itself beacuse it completely depends on your business logic which javascript is not aware of. You may use throw (we are going to see later in this article) to let the user know about the error.

Handling errors

Now, as we know about programming error types, let’s see how we can handle these errors in javascript. For the purpose we use try , catch, throw and finally statements. Whenever an error occurs the technical term we use for it is throwing an exception .


This allows you to test for errors in your code.You put the code you want to test within {}.


This block actually let’s you handle the errors thrown. The error object err has all the details about the errors.


Your code inside the finally block always gets executed after try and catch .It doesn’t depend on whether the code ran successfully or with some error.

Now, let’s understand them with an example


 Something went wrong
 TypeError: str.getNumber is not a function
    at window.onload ((index):35)
 This should always get executed


Sometimes you need to create custom errors and throw them when needed, may be when there is some logical error and you want the user to know something wrong happened.

Let’s see an example of an logical error and how we can use throw to throw an exception. In this example we are going to iterate an array and log each of it’s elements. At the end we are going to count the number of elements that are not printed(if any) and throw an exception with the count of not printed elements.


The number ,  2
The number ,  3
The number ,  4
The number ,  5
Uncaught 1 element(s) is/are not printed

onerror() method

This function fires the error event on window object whenever there is an error during runtime.Generally this is used with the syntax window.onerror


Some error occured
Uncaught ReferenceError: someFunction is not defined

Another usage of onerror() is to handle error while loading an image just list the below example.


I hope this article gives you basic understanding of errors and exceptions and how you can handle them in javascript. Bring on any question or feedback you have in the comments.

Happy Coding !!!

Adverts *