Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript error handling with try .. catch .. finally

I have a suspicion that I'm using the finally block incorrectly, and that I don't understand the fundamentals of its purpose...

 function myFunc() {       try {            if (true) {                 throw "An error";            }       } catch (e) {            alert (e);            return false;       } finally {            return true;       }  } 

This function will run the catch block, alert "An error", but then return true. Why doesn't it return false?

like image 631
nickf Avatar asked Nov 13 '08 05:11

nickf


People also ask

Does JavaScript have try catch finally?

Introduction to the JavaScript try… catch… finally statement. In this syntax, the finally block always executes after the try and catch blocks complete and whether exceptions occur or not.

Can we use try catch and finally together?

Answer: No. You cannot throw the exception and also catch it in the same method. The exception that is declared using throws is to be handled in the calling method that calls the method that has thrown the exception.

Can we use try catch in finally block?

No, we cannot write any statements in between try, catch and finally blocks and these blocks form one unit.

How do you handle errors in JavaScript?

JavaScript provides error-handling mechanism to catch runtime errors using try-catch-finally block, similar to other languages like Java or C#. try: wrap suspicious code that may throw an error in try block. catch: write code to do something in catch block when an error occurs.


2 Answers

The finally block contains statements to execute after the try and catch blocks execute but before the statements following the try...catch statement. The finally block executes whether or not an exception is thrown. If an exception is thrown, the statements in the finally block execute even if no catch block handles the exception. more

The finally block will always run, try returning true after your try block

function myFunc() {      try {          if (true) {                throw "An error";           }           return true;      } catch (e) {           alert (e);           return false;      } finally {           //do cleanup, etc here      }  } 
like image 185
Gilean Avatar answered Sep 18 '22 03:09

Gilean


Finally blocks execute when you leave the try block. In your code this happens when you return false. That sets the return value to false and attempts to exit the function. But first it has to exit the try block which triggers the finally and overwrites the return value to true.

It is considered by many to be a good programming practice to have a single return statement per function. Consider making a var retval at the beginning of your function and setting it to true or false as appropriate throughout your function and then structuring the code so that it falls correctly through to a single return at the bottom.

like image 20
Alan Oursland Avatar answered Sep 21 '22 03:09

Alan Oursland