Exception Handling in JavaScript / Try-Catch-Finally

Exception Handling in JavaScript / Try-Catch-Finally

Hello everyone, in this article we are going to talk about exception handling in Javascript. We will make examples with Try-Catch-Finally for better understanding.

Let's begin.

Sometimes errors occure during execution. These types of errors are called as Runtime errors These errors also called as Exceptions.

We handle runtime errors with Exception handling blocks.We write our code inside try block and if there is an exception occure execution goes to catch block and catch block is executed.

Below you can see the basic syntax of try-catch-finally:

try {
    // Main code block that will be executed
} 
catch(error) {
    // Code block when Runtime Error occured
}
finally() {
    // Code block will be executed in every situtation
}
Below you can see the concepts of blocks:
  • In try block we will locate the main codes.
  • If an error occured in try code, execution goes to catch block.
  • The code in finally block will executed in every situation.

Important: We have to use one of catch or finally blocks when try block used. Otherwise program will not be executed.

Let's make a simple example for better understanding.

function println(val){
     console.log(val);
}

try {
     println("Thecodeprogram");
     println(BurakHamdiTufan);
}
catch(error) {
    console.log('Error occured'); 
    console.log('Message: ' + error);  
}
Program output will be like below:

Thecodeprogram
Error occured
Message: ReferenceError: BurakHamdiTufan is not defined

As you can see above undeclared variable tried to be printed. But error is occured in runtime in try block and exception is handled in catch block.

Now let's make an example that includes a finally block:

As we mentioned above finally block being invoked at every situtation even if error occures or not.

Now let's write two Javascript examples. First example will go to catch block and secodn example will not. But, in both examples the finally block will be executed.

First Example:

try {
     println("Thecodeprogram");
}
catch(error) {
    console.log('Message: ' + error);  
}
finally {
    console.log('Finally method executed'); 
}
Program Output

Message: ReferenceError: println is not defined
Finally method executed
Second Example:

try {
     console.log("Thecodeprogram");
}
catch(error) {
    console.log('Message: ' + error);  
}
finally {
    console.log('Finally method executed'); 
}

Thecodeprogram
Finally method executed

We can also create custom exceptions. To do this we need to use throw new Error keyword. Below example you can see the example.


try {
     console.log("Thecodeprogram");
     throw new Error('Unexpected error occured'); 
}
catch(error) {
     console.log(error + "");  
}
Program output will be like below

Thecodeprogram
Error: Unexpected error occured

That is all in this article.

Have a good exception handling.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...