Skip to content
Related Articles

Related Articles

Define Exception handling in ES6

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 02 Nov, 2021

Exception: Exceptions are unwanted event that basically interrupts the normal flow of program. There are two types of exception in general:-

  • Synchronous (eg. runtime errors)
  • Asynchronous (eg. system errors)

Exception Handling: Often a times exception happens in a program that causes the program to terminate abruptly or in an unfriendly manner. To handle or to prevent the unanticipated behavior is called Exception Handling. Exception handling deals with synchronous exceptions such as bad user input, nonexisting files, etc. (it includes runtime errors).

Why Exception Handling: Exception handling basically ensures that the program does not terminate abruptly or the flow of the program does not break in an unfriendly manner. Meaningful error reporting is possible and one can generate understandable error reports for users. 

ES6  (ECMA Script Programming Language version 6) provides the important feature of exception handling. It is accomplished by using tryblock followed by either catchblock or finallyblock. As try block doesn’t exist alone, they are either followed by a catch block or by finally block. It exists in one of the three forms:-

  • try…catch
  • try…finally
  • try…catch…finally

1. try…catch block: Code or try statements in try block will execute first. Whenever the exception occurs it will be placed in the exception_var and the catch block will execute further.

Syntax: 

try {

    // try statements 
    // code to run
    
} catch (exception_var) {
    
    // catch statements
    // code to run 
    
}

2. try…finally block: First of all the try statements would be executed. After that finally, statements will be executed. Finally block will always get executed regardless of exception occurred or not.

Syntax:

try {

    // try statements
    // code to run
    
} finally {

    // finally statements
    // code that is always executed
}

3. try…catch…finally block:  try block executes at first if an exception occurs its value will be placed in exception_var and catch block will get executed after that finally block will get executed. However, finally block will execute regardless of conditions of the exception occurred or not. 

Syntax:

try {
 
   // try statements
   // code to run
   
 } catch (exception_var) {
 
     // catch statements
     // code to run if exception occurs
     
 } finally {
 
     // finally statements
     // code that is always executed
         
 }

Let’s understand with the below examples:

Example 1: Let’s take an example of bad user input in which the user divide the problem by zero “0”. 

javascript




<script>
    var num = 5;
    var de_num = 0;
    try {
        if(de_num == 0) {
            throw "Divide by zero error";
        } else {
            var sol = num / de_num;
        }
    } catch(e) {
        console.log("Error : " + e);
    }
</script>


Output: 

Error : Divide by zero error

Example 2: Let’s take another example in which a reference error is thrown whenever we use a reference of something that we have not declared. In this example, we have not declared the function intentionally and called it directly that will cause ReferenceError. 

javascript




<script>
    try{
      ab();
      // We have not declared the
      // function ab anywhere
    } catch(e){
      console.log("Error : "+ e.name);
    }
</script>


e.name will return the name of the error.

Output:

Error : ReferenceError

Example 3: In our final example we deliberately have written a statement to try to block syntactically wrong. We have not enclosed the string between single quotes properly. It will give us the SyntaxError.

javascript




<script>
    try {
        eval("alert('ES6 Exception Handling)");
    } catch(e){
        console.log("Error : " + e.name)
    }
</script>


Output :

Error : SyntaxError


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!