Questions tagged [finally]

Questions related to the finally block in try-catch construct.

278 questions
12
votes
2 answers

Property finally is missing in type Promise

I'm pretty sure I'll be able to solve this issue by myself but if it can help somebody else I want to share and save somebody else time. I had to add the es6-promise library to get rid of this error Promise only result to a type, but is only used…
Loic Coenen
  • 773
  • 3
  • 8
  • 19
12
votes
3 answers

object reference set to null in finally block

public void testFinally(){ System.out.println(setOne().toString()); } protected StringBuilder setOne(){ StringBuilder builder=new StringBuilder(); try{ builder.append("Cool"); return builder.append("Return"); }finally{ builder=null; /* ;)…
Mahendra Athneria
  • 1,203
  • 3
  • 16
  • 32
12
votes
5 answers

finally doesn't seem to execute in C# console application while using F5

int i=0; try{ int j = 10/i; } catch(IOException e){} finally{ Console.WriteLine("In finally"); Console.ReadLine(); } The finally block does not seem to execute when pressing F5 in VS2008. I am using this code in Console Application.
Mohammad Nadeem
  • 9,134
  • 14
  • 56
  • 82
11
votes
3 answers

Using finally block vs writing code after the try/catch block

As I understand, the following 2 examples should do the same thing. Why is the 1st considered better? 1: try { riskyMethod(); } catch(Exception e) { //handle exception } finally { cleanUp(); } 2: try { riskyMethod(); } catch(Exception e) { …
Voriki
  • 1,617
  • 2
  • 19
  • 43
11
votes
2 answers

Execute if no exception thrown

I have some code I want to execute if an exception is not thrown. Currently I'm doing this: try: return type, self.message_handlers[type](self, length - 1) finally: if not any(self.exc_info()): self.last_recv_time = time.time() Can…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
11
votes
1 answer

How to add a polyfill to support finally() in Edge?

I'm using axios library and using then(), catch() and finally(). Works perfectly in Chrome. However the finally() method does not work in MS Edge. I researched using polyfills or shims and I'm lost. I am not using webpack or transpiling and don't…
Craig
  • 770
  • 3
  • 14
  • 26
11
votes
5 answers

Proper replacement for the missing 'finally' in C++

Since there is no finally in C++ you have to use the RAII design pattern instead, if you want your code to be exception safe. One way to do this is by using the destructor of a local class like this: void foo() { struct Finally { …
Sascha
  • 1,410
  • 2
  • 10
  • 10
11
votes
6 answers

Java's strange behavior while returning from finally block

Try this piece of code. Why does getValueB() return 1 instead of 2? After all, the increment() function is getting called twice. public class ReturningFromFinally { public static int getValueA() // This returns 2 as expected { …
CodeBlue
  • 14,631
  • 33
  • 94
  • 132
11
votes
4 answers

Does try/finally ignore exceptions?

I have a situation where I want certain code to be executed no matter what happens, but I need exceptions to also be passed on up the stack to be handled later. Is the following: try { // code } finally { // code that must run } going to just…
Matt H
  • 7,311
  • 5
  • 45
  • 54
10
votes
11 answers

Where the finally is necessary?

I know how to use try-catch-finally. However I do not get the advance of using finally as I always can place the code after the try-catch block. Is there any clear example?
Tray13
  • 139
  • 4
10
votes
1 answer

Status of JSR/RET in JVM spec

There are some parts of the JVM specification which suggest that the operations JSR (Jump SubRoutine), JSR_W (Jump SubRoutine Wide) and RET (RETurn from subroutine) may be used only up to class file version 50.0 (JDK 1.6): 3.13 Compiling…
MvG
  • 57,380
  • 22
  • 148
  • 276
10
votes
4 answers

Get thrown exception in finally block

Is there a way, how to get currently thrown exception (if exists)? I would like reduce amount of code and apply some reuse for task looks like: Exception thrownException = null; try { // some code with 3rd party classes, which can throw…
TcKs
  • 25,849
  • 11
  • 66
  • 104
10
votes
5 answers

Close connection and statement finally

Which is better for finally block: finally { try { con.close(); stat.close(); } catch (SQLException sqlee) { sqlee.printStackTrace(); } } Or: finally { try { if…
Sajad
  • 2,273
  • 11
  • 49
  • 92
9
votes
4 answers

Does finally completely execute if an exception is thrown within finally block

so I have a bit of code here and I'm not sure entirely how it would react in the event that the reader.close() method throws an exception. public void someMethod(String s) throws IOException{ BufferedReader reader =…
ElvenAshwin
  • 183
  • 1
  • 7
8
votes
7 answers

Is finally block in C# a must?

What is the difference between 2 conditions? Every time when method1 or method2 runs, there should be a code block that is required to run. It seems to me that 2 method are the same. // example method1 void Method1(void) { try { //…
sanchop22
  • 2,729
  • 12
  • 43
  • 66