Introduction
In the previous chapter, we learned about Document Object Model methods with example programs.
In this chapter, we will learn about the Error and Exceptions Handling, and types of errors in JavaScript.
Errors and Exception Handling
There are three types of errors in the JavaScript programming language as follows:
- Runtime Errors
- Syntax Errors
- Logical Errors
We now learn this one by one.
Runtime Errors
It is nothing but the exception that occurs during the execution of a program like in other programming languages. The following example shows when you execute that there is no output shown:
Example - <!DOCTYPE html>
- <html>
- <title>Article By Jeetendra</title>
- <head>
- </head>
- <body>
- <script type="text/javascript">
- document.write("Runtime Errors");
- </script>
- </body>
- </html>
When you execute this it will display “Runtime Errors”. Just add the one line in that Print() function call like in the following it will display nothing.
Example
- <!DOCTYPE html>
- <html>
- <title>Article By Jeetendra</title>
- <head>
- </head>
- <body>
- <script type="text/javascript">
- Print();
- document.write("Runtime Errors");
- </script>
- </body>
- </html>
It will display nothing on the screen because we did not define a Print() method in JavaScript however we call that method. That's why it displays nothing on the screen.
Syntax Errors
The name defines everything about this error. It occurs during compiling. When you miss something like semicolon (;) at end of a statement yet there is a missing closing parenthesis. The following example shows it:
Example
- <script type="text/javascript">
- document.write("Syntax Errors</br>");
- function SynError()
- {
- document.write("Example of Syntax Error");
- }
- SynError();
- </script>
When you execute the preceding JavaScript code it will show output like the following:
- Syntax Errors
- Example of Syntax Error
Do some changes in the code like the following:
- <!DOCTYPE html>
- <html>
- <title>Article By Jeetendra</title>
- <head>
- </head>
- <body>
- <script type="text/javascript">
- document.write("Syntax Errors</br>");
- function SynError()
- {
- document.write("Example of Syntax Error");
-
- SynError();
- </script>
- </body>
- </html>
When you execute this JavaScript code it displays nothing on the screen because there is a syntax error caused by missing the closing curly braces of the function.
Logical Errors
This error is not a syntax or runtime error. It depends on your business logic. It is difficult to determine this type of error.
Exception Handling in JavaScript
This is a new feature of JavaScript for handling exceptions like in other programming languages, using try-catch finally statements, and the throw operator for handling exceptions. Now you can catch the runtime exception.
The following shows the syntax for a try catch finally:
- <script type="text/javascript">
- document.write("Exception Handling in JavaScript</br>");
- function ExceptHand()
- {
- try
- {
- document.write("This is try block");
- }
- catch(error)
- {
- document.write("This is catch block");
- }
- finally
- {
- document.write("It always execute");
- }
- }
- ExceptHand();
- </script>
The try block is used always before a catch block, because in a try block you provide your code to be executed. If there is no error in code then the catch block is not executed but the finally block is always executed. The following example shows exception handling:
Example
- <!DOCTYPE html>
- <html>
- <title>Article By Jeetendra</title>
- <head>
- </head>
- <body>
- <script type="text/javascript">
- document.write("Exception Handling in JavaScript</br>");
- function ExceptHand()
- {
- try
- {
- alert("This is try block");
- alert("Not present");
- }
- catch(error)
- {
- document.write(error);
- }
- }
- ExceptHand();
- </script>
- </body>
- </html>
When you execute this program it will first show an alert message box with the message “This is try block”. When it executes the next statement it is an error because there is no alert message box available for this exception catch by catch block and it prints the error object of the catch block. It will print the proper exception message like the following output:
Output
It will first show an alert message box with a message after you click OK the catch block catches the error and prints an error like the following:
Example for throw statement:
- <!DOCTYPE html>
- <html>
- <title>Article By Jeetendra</title>
- <head>
- </head>
- <body>
- <script type="text/javascript">
- document.write("Exception Handling in JavaScript</br>");
- function ExceptHand()
- {
- try
- {
- if(true)
- { throw("Example of throw statement");}
- }
- catch(error)
- {
- document.write(error);
- }
- }
- ExceptHand();
- }
- </script>
- </body>
- </html>
The preceding simple example shows the concept of the throw statement. When you execute the code it will check the condition for true then it will throw an exception with a message and it catches the catch statement and prints the thrown statement message.
Output
Summary
In this chapter, we learned about the concepts of errors and Exception Handling in JavaScript and how it works with example programs.