Broad Network

The try Statement in ECMAScript

ECMAScript Error Basics – Part 1

Foreword: In this part of the series I talk about the try statement that is used to catch errors in ECMAScript.

By: Chrysanthus Date Published: 22 Sep 2014


This is part 1 of my series, ECMAScript Error Basics. In this part of the series I talk about the try statement that is used to catch errors in ECMAScript.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at That is, contact me for the slightest problem you have about what you are reading.

Every computer language builds up. You need to learn something today in the language, and then use it to learn something at a higher level in the same language, tomorrow. This series is part of my volume, ECMAScript Course. At the bottom of this page, you have links to the different series you should have read before reaching here.

Programming Errors
There are three types of programming errors. In other words, there are three types of errors that can occur in a program. You have Syntax Errors, Logic Errors and Runtime Errors. Syntax error is when you type a statement wrongly. Logic error is when the program is correctly typed, but you mistakenly typed it to do what you did not intended. Runtime error is wrong input from the user or the system (network).

The try statement is to handle runtime errors. The other two types of errors are you fault; you just have to test the program and correct them.

Runtime Errors
Runtime errors occur when the program is being executed as a result of the fact that you forgot or did not take certain factor into consideration when coding. For example, let us say your code is to divide 8 by some denominator that the user inputs. If the user inputs 2, the division will work, giving you 4 as answer. If the user inputs zero, the division will not work, because 8/0 has no answer. When a runtime error occurs your program normally crashes (and stops). To solve runtime errors, you have to write code that will prevent the execution of the particular code segment from taking place. In this division example, you have to write code that will prevent division by zero from taking place, and possibly informing the user of the mistake he made by inputting zero as a denominator.

Preventing Runtime Errors
Runtime errors are prevented using what is called try-catch blocks. I will use the example of dividing 8 by a denominator to illustrate this. Consider the following code:


    <script type="text/ECMAScript">

        input = 2;

                if (input == 0)
                    throw "Division by zero is not allowed!";
                answer = 8 / input;
        catch (err)



Test the above code. The extra HTML code is to produce a web page. You should have 4 as the answer, displayed. Now change the value of the identifier, input at the beginning of the code from 2 to 0. You should have the text, “Division by zero is not allowed!” displayed.

The code divides 8 by the value of the input identifier. When the value of the input identifier is not zero, everything is fine. When the value is zero, that is an error, and so the program should not crash. Code has to be written to prevent the program from crashing.  

There are three things you should note about the code above. There is the try-block; there is the catch-block and there is a throw statement, which throws a string object (literal). The string is the error message to be displayed to the user.

The try-block begins with the reserved word, try, then you have the pair of curly brackets. The statements meant for the try block are in the curly brackets. The statements of the overall code that do division by zero, should be placed inside the try block.

The catch-block begins with the reserved word, catch. It has parentheses with a parameter. The parameter is an identifier of your choice. The catch-block has a pair of curly brackets. The statements meant for the catch block go into the curly brackets.

The very first statement in the try-block is an if-statement combined with what is called the throw statement? The idea is that you check if the input identifier value is zero. If it is zero, then the throw statement is executed to prevent crash. When the throw statement is executed, we say an exception is thrown. When an exception is thrown, the statements below the throw statement in the try block are not executed; while the statements in the catch-block must be executed; that is, when an error occurs, the statements below the throw statement in the try-block are not executed, while the catch-block must be executed to handle the problem. If no error occurs (in this case, input is not zero), then an exception will not be thrown. If an exception is not thrown, the statements below the throw statement in the try-block are executed, and the catch-block is not executed.

The try-block has the normal statements to be executed to solve the task required by the program. These statements are executed on condition that no error has occurred. The catch block has the statements to be executed if an error occurs. Usually what the catch block does is that it simply informs the user that an error has occurred with a brief description of the error; the rest of the program below the try-catch blocks continues to execute. If the error is detected in the try block and the catch block is executed as in the above code, then the program will not crash. Prevention of crash is as a result of the fact that the normal statements in the try block are not executed and the catch-block is executed. So, with the try-catch blocks, and error or no error, the code above and below the try-catch blocks are executed.

Remember, when there is no error, the throw statement is not executed; the catch block is also not executed; but the required statements in the try block are executed. The string thrown in the try block is the argument to the parameter, err of the catch block.

The try Statement Syntax
The try statement syntax is actually:

    TryStatement :
        try Block Catch
    Catch :
        catch ( Identifier ) Block
    Finally :
        finally Block

As you can see from the syntax, there are actually three blocks, not two: you have the try block, the catch block and the finally block. The above code does not need to have the finally block.

The finally Block
If the catch block cannot catch the error, then the finally block is called to do some default processing of the error. With that, your program is still safe, but the error is not handled, as it would have been, in the catch block. Errors can be very complicated. I have simplified the error situation in this tutorial. As you learn more in programming, you understand the complication of errors. Well, it is still you, the programmer to write the content of the finally block. Read and try the following code:

    <script type="text/ECMAScript">

        input = 0;

                if (input == 0)
                    throw "Division by zero is not allowed!";
                answer = 8 / input;
        catch (err)
                throw err;
                document.write("An error occurred with the division!");


The catch block could not solve the problem, so it simply re-threw the error object. The finally block gave a default or more generalized solution. Even if the catch block did not re-throw the exception, the finally block would still have handled the exception in the same way. The output is:

    An error occurred with the division!

Time to take a break. We stop here and continue in the next part of the series.


Related Links

ECMAScript Basics
ECMAScript Operators
Expressions and Statements in ECMAScript
Custom Object in ECMAScript
ECMAScript Global Object
The ECMAScript Array
The ECMAScript String Object
ECMAScript Regular Expressions
ECMAScript Date Object
ECMAScript Error Basics
Advanced Course
Number Objects in ECMAScript
Inheritance in ECMAScript
Advanced ECMAScript Regular Expressions
Mastering the ECMAScript (JavaScript) eval Function
More Related Links
Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course



Become the Writer's Fan
Send the Writer a Message