try-catch-finally

What is the point of the finally block?

Syntax aside, what is the difference between try { } catch() { } finally { x = 3; } and try { } catch() { } x = 3; edit: in .NET 2.0? ...

Why is try {...} finally {...} good; try {...} catch{} bad?

I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } catch // No args, so it will catch any exception {} reader.Close(); However, this is considered good form: StreamReader reader=new St...

Is it bad practice to return from within a try catch finally block

So I came across some code this morning that looked like this: try { x = SomeThingDangerous(); return x; } catch (Exception ex) { throw new DangerousException(ex); } finally { CleanUpDangerousStuff(); } Now this code compiles fine and works as it should, but it just doesn't feel right to return from within a try block,...

Claim resources/memory from thread single threaded apartment thread

I am using following single threaded appartment. I am unable to reclaim memory/other resources from thread object. Actullay I want to wrap my thread in try catch and fianlly block. try and catch are done. But I am unsure about finally block. What code, property or function do I need to call in finally block. System.Threading.Thread myTh...

Formatting of hard to read try..catch..finally blocks?

How are you formatting your try..catch.finally blocks? Especially when only wrapping it around a small amount of code, it blows everything and makes code pretty unreadable and unsightly in my opinion. Such as: try { MyService service = new Service(); service.DoSomething(); return something; } catch (Exception ex) { ...

Throw Exception VS Return Error within a Try,Catch,Finally

I'm pretty sure I already know the answer, but I'm still curious what the opinion is on handling an error within a Try,Catch,Finally block -- but when you're repeating yourself. BTW - I'm not talking about User Input - but using that for an example because it is clear and short Consider this bit of code... try { if (success) {...

Why does this "finally" execute?

If you run the code below it actually executes the finally after every call to the goto: int i = 0; Found: i++; try { throw new Exception(); } catch (Exception) { goto Found; } finally { Console.Write("{0}\t", i); } Why? ...

Why use Finally in Try ... Catch

I see that the Finally in try .. Catch will execute allways after any parts of the execution of the try catch block. Is it any different to just skip the Finally section and just run it after, outside the try catch block?? Example 1, Try ... Catch ... Finally ... End Try Try 'Do something Catch ex As Exception ...

Try-catch-finally and then again a try catch

I have often come across situations like :- try{ ... stmts ... } catch(Exception ex) { ... stmts ... } finally { connection.close // throws an exception } which still needs a try - catch block inside finally. What is the best practice to overcome this? ...

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 fairly high risk code in that it can fail due to the environment), but if I write try-catch...

How does Java's System.exit() work with try/catch/finally blocks?

I'm aware of headaches that involve returning in try/catch/finally blocks - cases where the return in the finally is always the return for the method, even if a return in a try or catch block should be the one executed. However, does the same apply to System.exit()? For example, if I have a try block: try { //Code System.exit(0...

How to explicitly pass a program flow into the finally block in C#?

Hi guys. In Delphi I could do something like this: try if not DoSomething then Exit; if not DoSomething2 then Exit; if not DoSomething3 then Exit; finally DoSomethingElse; end; In other means if method DoSomething results false then the program flow is transffered to the finally block and DoSomething2 and DoSomet...

What happens in a try-finally block with no catch when an exception is thrown?

What happens when an exception is thrown and you I have try-finally structure (without catch) What does this do? . public class Bicycle{ private int cadence; private int gear; private int speed; // the part with static:???? static{ something // I don't remember exactly the format } //...

Try Catch block in Siebel

I have a script which sends a set of records into a file. I'm using Try - Catch block to handle the exceptions. In the catch block I have a code where it has the pointer to next record. But this is not executing . Basically I wan to skip the bad record n move to next record. while(currentrecord) { try { writerecord event } catch { curr...

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 mechanism for halting program execution in unexpected error situations (maybe someone could give...

Ensure teardown runs in Test::Unit::TestCase?

I'm using Test::Unit::TestCase to write some unit tests. Currently, I have a setup function that moves and modifies some fixture files and folders on disk. (This is a necessary evil at the moment.) If a test crashes, the teardown method doesn't get called, leaving files and folders in the way. The next time I run a test, it complains...

Use case for try-catch-finally with both catch and finally

I understand how try-catch works and how try-finally works, but I find myself using those (usually) in two completely different scenarios: try-finally (or using in C# and VB) is mostly used around some medium-sized code block that uses some resource that needs to be disposed properly. try-catch is mostly used either around a single s...

Try Catch - Finally in If statement, how to go on after ?

how can I do that ? void x() {.... if (...) {try {} catch (ComException com) { throw com} finally // in any case, executed fine! {...instructions.......} } ... instructions...// not executed in case of exception because the finall...

How to make a try-catch block that iterates through all objects of a list and keeps on calling a method on each of them until there are no more exceptions to catch?

Basically iterating through a list and, - Invoke method on first object - Catch first exception (if any); if there are no more exceptions to catch, return normally. Otherwise, keep on invoking method until all exceptions are caught. - Move on to next object. I can iterate through each object, invoke the method, and catch one exception b...

does code in finally get run after a return in objective-c?

consider the following code: @try { if (something.notvalid) { return; } // do something else } @catch (NSException *ex) { // handle exception } @finally { NSLog(@"finally!"); } if something is not valid and i return from within the try, does the code in @finally execute or not? I believe that it should but others I've...