JavaScript Error/ Exception Handling 2020

In this article, we will understand how Error Exception Handling done In JavaScript and different types of errors.

Error Handling / Exception Handling In JavaScript:

Error is an action that is incorrect and it might occur due to some misconfiguration or invalid input from a user or while performing some operation in which is data is not a proper format in which operation is expected.

Because of an error, your program will terminate abnormally. To avoid such a situation we need to catch such error or exception. If the program terminates abnormally then the user will frustrate to avoid such a situation you need to handle such kind of errors or exceptions in the JavaScript program.

To handle such a situation in javascript it will provide some keywords/ methods which are try, catch, finally and throw. We will understand the meaning of each term in the below information.

Types of Errors:

Mainly 3 types of error occurred in the program.

  1. Syntax error
  2. Logical Error
  3. Runtime Error

Syntax Error

As per computer science definition a syntax error is an error in syntax and which meanwhile you writing a program you sequence of characters or tokens might misplace or you forgot to enter.

<script type="text/javascript"> 
  
      // Here the semicolon after the printing statement 
      //  is missing and it is a syntax error 
      window.print()   
  
</script> 

Logical Error

It is the most difficult error to trace and error mainly because of the logical part of coding or logic which called a bug. That bug causes the program to terminate abnormally.

Runtime Error

This error occurred during the program execution or program is running is also known as an exception.

<script type="text/javascript"> 
  
      // An runtime error here  
      window.addition();  
  
</script> 

In the above program, there is no addition method present in window object we called here so this will cause a runtime exception.

  1. Try: In this try block you can write a code that you thought might cause an exception. This means it is a test block.
  2. Catch This statement used to catch exception as the name suggest. If your try block causing exception it will catch in the catch block.
  3. Finally: This statement is always executed irrespective of whatever happened In the program. It is generally used after try or try..catch block so this block ensures you I will execute 100%.
  4. Throw: This is used to throw a custom exception or user-defined exception which end-user can understand. It means it allows you to create custom errors as per the user’s need.
  5. Error object: it is a built-in error object that provides you information about an error that occurs. The error object provides you 2 useful properties name and message.

Error Object Properties

Property Description
name Sets or returns an error name
message Sets or returns an error message (a string)

Error Exception handling In JavaScript
Error Exception handling In JavaScript

Window.onerror in JavaScript

In general, we handle the error using try..catch block in JavaScript. Suppose the code which causes an exception is not present inside the try…catch block then onerror event is fired.

The onerror is the property of GlobalEventHandlers that processes the request.

General Syntax of window.onerror function is

window.onerror = function(message, url, lineno, colno, error) { ... }

  1. Message: it specifies the error message
  2. URL: it specifies the location of the file where the error occurred
  3. Lineno: it specifies line number where the error occurred
  4. Colon: it specifies the column number of the line where an error occurred
  5. Error: it is an error object

What is the significance of return true in the window.onerror function?

In the older version of the javascript function, it displays errors on the bottom right corner of the webpage or browser window. If you want to suppress this you need to return true. Else you can return false from this function.

window.onerror=function(message, url, lineno, colno, error)
{
    document.writeln("Message : " + message);
    document.writeln("<br/>");
    document.writeln("URL : " + url);
    document.writeln("<br/>");
    document.writeln("lineNo : " + lineno);
    document.writeln("<br/>");
    document.writeln("colno : " + colno);
    document.writeln("<br/>");
    document.writeln("error : " + error);

    return true;
}

IcallNeverExistingFunction();

window.onerror
window.onerror

GitHub: https://github.com/Sagar-Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...