Questions tagged [try-catch-finally]

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

435 questions
15
votes
5 answers

Using statement and try-catch()-finally repetition?

The using(...) statement is syntactic sugar for try{} finally {}. But if I then have a using statement like below: using (FileStream fs = File.Open(path)) { } Now I want to catch the exceptions that opening this file could cause (and this is…
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
14
votes
2 answers

python: recover exception from try block if finally block raises exception

Say I have some code like this: try: try: raise Exception("in the try") finally: raise Exception("in the finally") except Exception, e: print "try block failed: %s" % (e,) The output is: try block failed: in the…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
13
votes
5 answers

Uncaught RuntimeException and finally clause: which comes first?

A RuntimeException is thrown in try block without being caught, while the finally clause invokes System.exit(). public static void main(String[] args) { try { Integer.valueOf("NotANumber"); } finally { …
chance
  • 6,307
  • 13
  • 46
  • 70
13
votes
4 answers

Is there such case when in try\finally block the finally won't be executed?

I'm studying for my test in Object Oriented Programming and I was wondering if there is any case what so ever that considering the following code: try { do something } catch (someException e) { } finally { do something } the finally block…
Dave
  • 141
  • 3
13
votes
3 answers

How try...finally works internally

Currently I am working on code optimization where I am using try.. finally block to deference my objects. But I have confusion that how returning an object is managed when I am creating null reference to an object in my finally block. ?? While…
Pratik
  • 944
  • 4
  • 20
13
votes
4 answers

Java - If I return in a catch block, will the finally block be executed?

This is what I'm trying to do: try { //code } catch (Exception e) { return false; } finally { //close resources } Will this work? Is it bad practice? Would it be better doing this: boolean inserted = true; try { //code } catch…
Comic Sans MS Lover
  • 1,729
  • 5
  • 26
  • 52
12
votes
7 answers

When to use and when not to use Try Catch Finally

I am creating asp.net web apps in .net 3.5 and I wanted to know when to use and when not to use Try Catch Finally blocks? In particular, a majority of my try catch's are wrapped around executing stored procs and populating textfields or gridviews?…
user279521
  • 4,779
  • 21
  • 78
  • 109
11
votes
5 answers

Is this `try..catch..finally` redundant?

public Foo doDangerousStuff() throws Exception { try { dangerousMethod(); return new Foo(); } catch (Exception e) { throw e; } finally { mustBeCalledAfterDangerousMethod(); } } Does this behave any…
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
11
votes
4 answers

Using Exception Handling versus NSError in Cocoa Apps

Hey all. I've been reading up on Apple's suggestions for when/where/how to use NSError versus @try/@catch/@finally. Essentially, my impression is that Apple thinks it best to avoid the use of exception handling language constructs except as a…
user244050
11
votes
10 answers

Difference between finally and write after catch

What is the difference between "finally" and write after "catch"? For example: public boolean example() { try { // Code } catch (RuntimeException e) { // Code } finally { return true; } } public boolean…
Milton90
  • 547
  • 2
  • 7
  • 15
11
votes
3 answers

Powershell Try Catch invoke-sqlcmd

I am having problems catching an error in PowerShell when a connection fails to a SQL Server using Invoke-Sqlcmd. This is some generic code to demonstrate the issue: CLS $server = "Localhost\fake" try { Invoke-Sqlcmd -Query "SELECT DB_NAME() as…
Eric Z
  • 113
  • 1
  • 1
  • 4
10
votes
3 answers

Behavior of return statement in catch and finally

public class J { public Integer method(Integer x) { Integer val = x; try { return val; } finally { val = x + x; …
sachin
  • 1,447
  • 4
  • 14
  • 22
10
votes
3 answers

What is the 'defer' equivalent for Java

This is just a short example of Go code: package main import "fmt" func main() { defer fmt.Println("world") //use of keyword 'defer' fmt.Println("hello") } I am finding an equivalent of 'defer' in Java. Instead of 'defer' I can use try…
Romeo
  • 115
  • 2
  • 11
10
votes
5 answers

Does code in a finally get executed if I have a return in my catch() in c#?

I have the following code snippet / example. It's not working code I just wrote this so as to ask a question about catch, finally and return: try { doSomething(); } catch (Exception e) { log(e); return Content("There was an…
Angela
  • 3,269
  • 3
  • 22
  • 24
9
votes
1 answer

try catch in finally section

Is it considered bad programming to write a try and catch within a finally clause? I'm having in my main method a fileInputStream which I want to close. I want to place the .close() in the finally, so it will close no matter what. I don't want to…
La bla bla
  • 8,558
  • 13
  • 60
  • 109