Try and Throw Statements in ECMAScript
Statements in ECMAScript – Part 4
Foreword: In this part of the series I talk about ECMAScript statements to handle runtime errors.
By: Chrysanthus Date Published: 13 May 2016
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:
input = 2;
if (input == 0)
throw "Division by zero is not allowed!";
answer = 8 / input;
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.
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.
There is more to the try and throw statements than I have talked here - see later.
Let us stop here and continue in the next part of the series.
Related LinksECMAScript Basics
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
Scopes in ECMAScript
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
More Related Links
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module