Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript try...catch...else...finally like Python, Java, Ruby, etc

How can Javascript duplicate the four-part try-catch-else-finally execution model that other languages support?

A clear, brief summary is from the Python 2.5 what's new. In Javascript terms:

// XXX THIS EXAMPLE IS A SYNTAX ERROR try {   // Protected-block } catch(e) {   // Handler-block } else {   // Else-block } finally {   // Final-block } 

The code in Protected-block is executed. If the code throws an exception, Handler-block is executed; If no exception is thrown, Else-block is executed.

No matter what happened previously, Final-block is executed once the code block is complete and any thrown exceptions handled. Even if there’s an error in Handler-block or Else-block and a new exception is raised, the code in Final-block is still run.

Note that cutting Else-block and pasting at the end of Protected-block is wrong. If an error happens in Else-block, it must not be handled by Handler-block.

like image 581
JasonSmith Avatar asked Feb 02 '11 08:02

JasonSmith


People also ask

Does JavaScript have try-catch finally?

finally statements combo handles errors without stopping JavaScript. The try statement defines the code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result.

How does try-catch finally work in JavaScript?

The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed. The code in the finally block will always be executed before control flow exits the entire construct.

Is try-catch good practice JavaScript?

Don't Overuse the “Try-Catch” However, one of the common mistakes developers make is to overuse exception handling. Sometimes we do it to keep the code look consistent across different files and methods. But, unfortunately, these would cause adverse effects both for application performance and error detection.

What can I use instead of try-catch in Java?

But anyhow, even without them, Vavr Try is a real alternative for Java try-catch blocks if you want to write more functional-style code.


2 Answers

I know this is old, but here is a pure syntax solution, which I think is the proper way to go:

try {     // Protected-block     try {         // Else-block     } catch (e) {         // Else-handler-block     } } catch(e) {     // Handler-block } finally {     // Final-block } 

The code in Protected-block is executed. If the code throws an error, Handler-block is executed; If no error is thrown, Else-block is executed.

No matter what happened previously, Final-block is executed once the code block is complete and any thrown errors handled. Even if there’s an error in Handler-block or Else-block, the code in Final-block is still run.

If an error is thrown in the Else-block it is not handled by the Handler-block but instead by the Else-handler-block

And if you know that the Else-block will not throw:

try {     // Protected-block     // Else-block } catch(e) {     // Handler-block } finally {     // Final-block } 

Moral of the story, don't be afraid to indent ;)

Note: this works only if the Else-handler-block never throws.

like image 109
cbarrick Avatar answered Sep 22 '22 10:09

cbarrick


Extending the idea of jhs a little, the whole concept could be put inside a function, to provide even more readability:

var try_catch_else_finally = function(protected_code, handler_code, else_code, finally_code) {   try {     var success = true;     try {       protected_code();     } catch(e) {       success = false;       handler_code({"exception_was": e});     }     if(success) {       else_code();     }   } finally {     finally_code();   } }; 

Then we can use it like this (very similar to the python way):

try_catch_else_finally(function() {   // protected block }, function() {   // handler block }, function() {   // else block }, function() {   // final-block }); 
like image 30
Jakob Avatar answered Sep 18 '22 10:09

Jakob