Menu Close

Articles

JavaScript: exceptions

Exceptions are abnormal situations that can cause a computer program to malfunction, produce unpredictable or incorrect results. In this case the application should report an error message and interrupt itself or at least verify the situation and decide to stop or continue.

Producing exceptions

An exception can be created by the system when a statement can not be executed properly. You can also create exceptions on purpose. Usually you check a condition and decide before producing an exception.

Most elegant is to produce an Error object as exception, but JavaScript enable you to produce an exception using any value. The example below will use keyword: throw to create an exception.

Example:

Homework:  This example will stop on first statement, but you can comment statements one by one to check the effect of each individual case. The script will stop and you get an error message similar to this:

error: Uncaught -1

Resolving exceptions

Unresolved exceptions cause script to stop and produce an error to console, but your web-page will usually continue working with defects. To avoid these defects you must do proactive programming. This means you must check conditions and handle errors to avoid “Uncaught Exceptions”.

For resolving exception you can use a special “control flow” statement that is called “try”. It is a multi-block statement with several regions: The main block start after “try” keyword. You can use “catch blocks” to resolve several potential errors and “finally” block to execute after error is resolved.

Syntax:

Notes:

  • Square parentheses are not part of the syntax. They show you optional blocks, 
  • Ellipsis symbol “…” is not part of syntax. They symbolize multiple “catch” blocks.

Example:

Notes:

If you use: console.error() instead of console.log, the console will probably print your error message with red color. You can use a function to do something with the error. Usually you log the error and continue program execution.

Handler

The catch statement has a handler that is created automatically. You can specify this handler name in parenthesis after catch keyword. In our case the exception handler is: e. Handler type depends on your throw statement. If you create an Error object the handler will represent the object. In our case, e is of type ‘string’.

Conditional catch

You can create different catch statements using a conditional. Notice though this is very new and it may bot be supported by many engines. Check with your team before you use this syntax.

Pattern:

Next is “pseudo code” fragment to demonstrate “conditional catch” design pattern.

Finally

The finally block is executed no matter what. In this block you can close a resource or log a message. When in this block you do not know if there was an error or not, but this code is guaranteed to be executed in any case.

Pattern:

Next pattern demonstrate how to handle resources using try – finally block.

Error 

The Error instance must be created using “new” operator like any other object. You can create an exception object and “throw” the object later once or several times, depending on your programming logic. Also you can create the object anonymously and give it a name later using “catch”.

Example:

Note: To learn more about Error object read the reference: MDN Error

Read next: Web pages