Using finally expression

This is a very simple question. In Java, I use a finally expression to close resources, because "this is good practice." I developed in Javascript and then in Node.js for several years, and never used the finally statement. I know that in Node.js we all follow the first error handling pattern. In any case, the following two snippets will do the same:

 try{ throw 123 }catch (e){ }finally{ console.log(1) } 

.

 try{ throw 123 }catch (e){ } console.log(1) 

Both print 1.

Why finally keyword if it has no real benefit? The cleaning code can be placed inside the catch.

+20
source share
9 answers

Finally, it is useful not only for handling exceptions - it allows the programmer to avoid accidentally bypassing the cleanup code when returning, continuing, or breaking.

Just a simple and straightforward example that shows the difference. There is a return that violates the completion of the function, but console.log in finally is called while the last console.log is skipped.

 let letsTry = () => { try { // there is a SyntaxError eval('alert("Hello world)'); } catch(error) { console.error(error); // break the function completion return; } finally { console.log('finally') } // This line will never get executed console.log('after try catch') } letsTry(); 
+15
source

But try this:

 try { throw "foo" } catch (e) { throw "bar" } finally { console.log("baz") } console.log("quux") 

If a second error is catch from the catch , the code after the try...catch will not be run.
The finally block always starts, even if an error is detected in the catch block.

In addition, the finally block is executed even if the return or break return stops the code in the try or catch . The return in the finally block override the return in the try or catch .

 function foo() { try { return "bar"; } finally { return "baz"; } } foo() // "baz" 
+5
source

oracle docs give a good answer to this. Bottom line: finally, always called! Even if you catch only one kind of exception (and not a global catch), then it is finally called (after which your application will probably break if there is no other catch)

+4
source

The finally block is for special purposes.

finally useful not only for handling exceptions - it allows the programmer to avoid accidentally bypassing the cleanup code by returning, continuing, or breaking. Putting the cleanup code into the finally block is always good practice, even if no exceptions are expected.

Since this will not affect your business logic, it is nonetheless a compiler friendly in terms of memory.

+1
source

What if the try block returns early or throws an exception that you are not handling? You still want to free up the resources that you have allocated, right?


EDIT:

The answers to this question seem almost philosophical, there are some "guesses", and basically "we believe that it should be useful because it exists, therefore it should be used," and "even Oracle says so." Or maybe it helps the programmer not to "forget something" or "accidentally exit and not realize it."

These are almost all good reasons, but there is also a technical reason.

This helps to avoid code duplication in the mentioned cases when (a) either try is returned, or one of the catch blocks, or (b) if a second exception is thrown in the catch block.

In these cases, if some cleanup code or any other code that still needs to be executed after the return and after the second exception can be placed in the finally block, if it should be executed both after the try block and after the catch block,

You can still do this without the finally block, but the code will have to be duplicated, which the finally block avoids. This is where you really need to.

So, if you are sure that you will not miss it as case (a) or (b), you can still put the 'finally' code after the try / catch block and omit the finally clause.

But what if the situation changes? When you or another person change the code at a later stage, you may forget to check if the cleanup code is missing in any situation now.

So why not always put the cleanup code in a finally block? And this is what is recommended and what many JavaScript programmers do.

+1
source

You use it when you want to make sure your code runs at the end, even if there was an exception at runtime:

 InputStream is = new FileInputStream("C://test.txt"); try { //code... } catch (Exception e) { //code... } finally { is.close(); } 
+1
source

This is a very good question.

In javascript, it makes no sense to use finally , but I can imagine situations in which this might be useful.

Suppose you have a web page where you display a specific div after some user action, for example. button is pressed.
div shows some entries, for example, for the action that the user requested.
Upon completion of the action (error or lack of error) you want to hide the div again. You can use the finally clause for this.

 function doSomething() { var d = document.getElementById("log"); show(d); try { ... execute action ... } catch(e) { log(e); } finally { hide(d); } } 

In general, as you already mentioned, exceptions are used in JavaScript in favor of error callbacks. Thus, one might also ask what are the benefits of exceptions in JavaScript in general.

+1
source

In Java, if an exception occurs that does not match any of the locks, it will be broken and all open resources will be left open. The finally block will always be executed, even if an uncaught exception occurs.

0
source

Perhaps for this case

 try{ throw 123 } finally { console.log(1) } 
-2
source

Source: https://habr.com/ru/post/1497217/


All Articles