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
22
votes
6 answers

advice on nested Java try/finally code sandwiches

I would like some advice on a technique I bumped onto. It can be easily understood by looking at the code snippets, but I document it somewhat more in the following paragraphs. Using the "Code Sandwich" idiom is commonplace to deal with resource…
xtofl
  • 40,723
  • 12
  • 105
  • 192
22
votes
2 answers

Will the 'finally' block fire even after a Junit test throws an Assertion Error from with in 'try' block?

Will the writer.close() method inside the finally { } block run on an Junit Assertion Error? Assume the following code: @Test public void testWriter() { try { writer.open(); final List myBeans = new…
Hari Krishna Ganji
  • 1,647
  • 2
  • 20
  • 33
22
votes
1 answer

await in try-finally block

I've been playing around with the Visual Studio 14 CTP 2. This version of C# vNext enables the use of the await keyword inside a finally block. I am trying to figure out how this was implemented. I know this is an implementation detail and is…
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
20
votes
6 answers

Best practice to do nested TRY / FINALLY statement

Hi What is the best way to do nested try & finally statements in delphi? var cds1 : TClientDataSet; cds2 : TClientDataSet; cds3 : TClientDataSet; cds4 : TClientDataSet; begin cds1 := TClientDataSet.Create(application ); try …
Charles Faiga
  • 11,665
  • 25
  • 102
  • 139
19
votes
1 answer

Using "try"+"finally" without "except" never generates any error

I thought that if I use "try" and just "finally" else, without any "except", if the "try" statements couldn't be executed, the "finally" statements should be executed, but after that, an error should be shown in the execution, but in this simple…
Ali Rojas
  • 549
  • 1
  • 3
  • 12
15
votes
5 answers

Closing nested Reader

When reading from a text file, one typically creates a FileReader and then nests that in a BufferedReader. Which of the two readers should I close when I'm done reading? Does it matter? FileReader fr = null; BufferedReader br = null; try { fr =…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
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
11
votes
6 answers

Using finally instead of catch

I've seen this pattern a few times now: bool success = false; try { DoSomething(); success = true; } finally { if (!success) Rollback(); …
configurator
  • 40,828
  • 14
  • 81
  • 115
10
votes
4 answers

c# yield and try-finally

If I have a coroutine as follows, will the code in the finally block get called? public IEnumerator MyCoroutine(int input) { try { if(input > 10) { Console.WriteLine("Can't count that high."); yield break; } …
Liron
  • 2,012
  • 19
  • 39
10
votes
1 answer

Delphi - What is the "correct" order for except and finally blocks?

Suppose I have the following routine: function ReadFile(f : TFilename) : Boolean; var fs : TFileStream; begin Result := False; try fs := TFileStream.Create(f, ...); try // read file ... Result := True; finally …
rossmcm
  • 5,493
  • 10
  • 55
  • 118
9
votes
3 answers

How to simulate try-finally or try-except in languages that don't have them

Is there any way to simulate a try-finally or try-except in a language that doesn't have them? If there's some random, unpredictable, exception happens i need to be sure some cleanup runs. i could try to be sure that no exception in thrown, that way…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
8
votes
2 answers

Empty finally{} of any use?

An empty try has some value as explained elsewhere try{} finally { ..some code here } However, is there any use for an empty finally such as: try { ...some code here } finally {} EDIT: Note I have Not actually checked to see if the CLR has…
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
8
votes
2 answers

In Java when is the final block in a constructor executed?

For example: /** * Constructor */ public Test(InputStream in){ try{ this.inputStream = in; } finally{ inputStream.close(); } } Is the InputStream that is passed to the instructor immediately closed after the Test object is…
nomnom
  • 1,560
  • 5
  • 17
  • 31
7
votes
3 answers

Is it an error to return a value in a finally clause

If I try the following code, I see that the normal block return value is not returned, but the finally block return value is: >>> def f(): ... try: ... return "normal" ... finally: ... return "finally" ... >>>…
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
7
votes
2 answers

Using try-finally block inside while loop

I am trying to understand the mechanism when i use finally inside a while loop. In the below code. In finally line prints and than the while breaks. I was expecting the code not to reach the finally block. Or if it reaches the finally block, there…
rematnarab
  • 1,277
  • 4
  • 22
  • 42
1
2
3
12 13