Questions tagged [try-finally]

try-finally is a clause used to define a block of code which may throw an exception along with instructions to execute regardless of whether an exception occurs or not.

References

182 questions
1
vote
2 answers

Do you need a catch block if all you're doing is throwing the caught exception?

I found some code where they want to propagate an exception, but they want to run some clean-up code beforehand, so naturally it uses Try/Catch/Finally. But... they aren't actually doing anything with the exception, only forwarding it on. It was my…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
1
vote
0 answers

correctly closing input stream chains

I'm reading a file containing binary data followed by a serialized object: FileInputStream fis = new FileInputStream(file); GZIPInputStream gzis = new GZIPInputStream(fis); DataInputStream dis = new DataInputStream(gzis); ObjectInputStream ois = new…
DutChen18
  • 1,133
  • 1
  • 7
  • 24
1
vote
2 answers

NullPointerException thrown after finally block completes

I'm trying to make an Android game, and I am following a few code samples to get my game loop working. It involves making a new thread. In the run() method I have a try/finally block. After the finally block executes a NullPointerException is…
robev
  • 1,909
  • 3
  • 23
  • 32
1
vote
3 answers

Why finally block may not execute when exception is thrown?

For a long time I thought that it allows me to free up all the resources in the finally block and I thought that if an exception happens in the try block, then the resources will still be free up in the finally block. But that seems not to be the…
qqqqqqq
  • 1,831
  • 1
  • 18
  • 48
1
vote
1 answer

Doing clean-up inside finally block, with return in try block - bad practice?

I have this code to figure out the width of some text: function textWidth(text, font) { const span = document.createElement('span') try { span.style.cssText = `position: absolute; opacity: 0; font: ${font || "'Arial'"};` …
Magnus Buvarp
  • 976
  • 7
  • 19
1
vote
2 answers

Is it a good/right practice to use Finally keyword to prevent the JVM from re-ordering?

I just think of this question, and I have not found a similar question asked anywhere. I can close this one if there is a duplicate that I have not found. Here is a simple example: Assuming this is a multi-threading scenario, and in a writer thread,…
Lubor
  • 989
  • 3
  • 10
  • 33
1
vote
0 answers

Awareness of exception in finally block, but without catch block

Is it possible to determine if an exception was thrown without a catch block? Visual Studio is able to mine the exception out of my program state and put it as a pseudo-variable ($exception). Can I do something similar? I'm working with external…
Jamaic
  • 11
  • 2
1
vote
1 answer

Exception Handling : Getting different output in each run for same code

I am getting different output in each run of my program. When executed first time, it gives When executed again, it Please tell me why it is occurring. public class TwoExcepProg { public static void main(String[] args) { try { …
1
vote
3 answers

Execution order of try, catch and finally block

Suppose I have some C# code like this: try { Method1(); } catch(...) { Method2(); } finally { Method3(); } Method4(); return; My question is, provided no exception is thrown, will Method3() be executed before Method4(), or is it that…
serious6
  • 197
  • 1
  • 2
  • 7
1
vote
3 answers

try-finally block continue for loop

I'm writing a loop that ignored the Exception and it works well. for (; flag; ) { try { //do something ignore exception. Runnable r = queue.pollFirst(); r.run(); } catch (Exception ignored) { …
user4003256
1
vote
3 answers

Different behaviors when return place inside and after try

I have following two code block, which I use to compress a String. code 1 public static String compressResponse(String response) throws IOException { Deflater deflater = new Deflater(Deflater.DEFLATED, true); ByteArrayOutputStream…
1
vote
1 answer

Performance difference between 'with open(file):' and just opening/closing it manually

I think everyone will agree that with open(file_name, 'mode') as f: #do things with f is way better than f = open(file_name, 'mode') #do things with f f.close() From http://effbot.org/zone/python-with-statement.htm we can read When the…
user5457708
1
vote
1 answer

When Does the Finally Block Run Relative to the Return

I stumbled across an interesting error yesterday and have since fixed it, but it still was bothering me this morning, so I would like to see if anyone can shed some light on the issue. The code in question: final ResultSet rs =…
Alex N.
  • 654
  • 8
  • 21
1
vote
3 answers

FURTHER CLARIFICATION: How to correctly write Try..Finally..Except statements?

RE: How to correctly write Try..Finally..Except statements? I'm still confused by the OP's original question. Specifically, the last line of the procedure (outside of the try..finally..end) that reads "Screen.Cursor:=crDefault". My understanding is…
Vin Colgin
  • 19
  • 1
  • 2
1
vote
2 answers

Try and finally in python

def connect(self): ok = False try: conn = ftplib.FTP(self.hostname, self.user, self.password) ok = True return conn finally: if not ok: logging.error('Failed to connect to %s for %s' %…
nialloc
  • 805
  • 7
  • 17