Broad Network


Error Basics and ECMAScript 2015

ECMAScript Basics - Part 14

ECMAScript 6

Foreword: In this part of the series, I talk about programming errors with ECMAScript.

By: Chrysanthus Date Published: 12 May 2016

Introduction

This is part 14 of my series, ECMAScript Basics. In this part of the series, I talk about programming errors with ECMAScript.

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 Errors
This is the wrong use of syntax. These errors are wrong statements. When you type a statement, which is wrong, that is a syntax error. Such a statement cannot be executed. For example, in a statement you may unknowingly type an identifier beginning with a number. ECMAScript has a rule that no identifier should begin with a number. So if you start an identifier with a number that is wrong syntax. Under this condition, your program does not work. With a syntax error, the program is not executed. Before ECMAScript code is executed, there is some minimum compilation that takes place. Syntax errors would be spotted by the ECMAScript compiler and execution (interpretation) of the program will not take place.

Logic Errors
In this case, ECMAScript parser understands your program very well and it executes the program. However, the program will not do what you thought it should do. It will do something slightly different or completely different. The fault is yours. For example, a loop that is required to do 10 iterations might do 5 iterations, because you coded it mistakenly to do 5 iterations. Another example is that a loop might iterate infinitely, because the condition you gave for the loop is wrong. Logic Errors occur when the program is being executed (interpreted). The only way to solve this problem is to test your program very well before you hand it to the customer (who asked for it).

Runtime Errors
Runtime errors occur when the program is being executed as a result of the fact that you 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 error, 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 a try-catch construct. I will use the example of dividing 8 by a denominator to illustrate this. Consider the following code:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

    <script type="text/ECMAScript">

        input = 2;

        try
            {
                if (input == 0)
                    throw "Division by zero is not allowed!";
                answer = 8 / input;
                document.write(answer);
            }
        catch (err)
            {
                document.write(err);
            }    

    </script>

</body>
</html>

The extra HTML code is to produce a web page. Test the whole code. 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 braces. The statements meant for the try block are in the curly braces. 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 braces. The statements meant for the catch block go into the curly braces.

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 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 construct, and error or no error, the code above and below the try-catch construct 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.

Well, let us take a break here and continue in the next part of the series.

Chrys

Related Links

ECMAScript Basics
ECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message