Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Quickie question on exceptions in C#

If I rethrow an exception in the catch block is the finally block still executed?

AMS
Saturday, January 15, 2005

Um...can't you just put a Debug.Write statement in the Finally section and try it out?

Kyralessa
Sunday, January 16, 2005

Yes.  The finally will always be executed no matter what happens in the try or catch.

smallbiz
Sunday, January 16, 2005

Thanks.  That was my understanding but just wanted to confirm.  Yeah I supposed I could have just tried it.

As a followup, is it possible to ever get into an exception loop?  E.g. some pattern of method invocations that ends up with an exception being thrown around in circles?

Example, method 1 invokes method2, method 2 has an exception, but its exception handling logic invokes method 1.  Does the runtime detect and break this?

AMS
Sunday, January 16, 2005

There's nothing specific in the runtime to detect/break recursion - whether or not exception handlers are involved.

Joe
Sunday, January 16, 2005

AMS - do you have some deeply rooted fear of experimentation? Both questions could have been answered yourself within about 5 minutes. I think you need to get IN more.

gwyn
Tuesday, January 18, 2005

Well, we don't need to be rude about it.  I know I too have a tendency, which I often have to fight against, to look for a quick answer to a question that I could easily try out to see.  The difference is that if I search Google Groups for the answer to a question like this one, I'm trusting someone else for the answer, while if I try it out, I know definitively.

Now if you can get an answer online and then try it out to see that it works, that's one thing.  But if it will make a difference in your code, then test, rather than trust someone you don't know who bears no responsibility for your code.

Kyralessa
Tuesday, January 18, 2005

Yeah. Sorry, I wasn't actually trying to be rude, believe it or not. Should have put an exclamation mark on the end.

gwyn
Wednesday, January 19, 2005

I never saw the reason behind the finally block. Isn't the code after the try/catch statement always going to be executed anyway?

C O'Connor
Wednesday, January 19, 2005

Only if you catch all exceptions.

The point of the try/finally is for when you have exceptions you aren't catching. In fact, it's perfectly legal -- and extremely common -- to have try/finally without any catch statements at all.

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, January 19, 2005

Oh, and I agree with the assessment that the OP needs to learn to write test code. These were literally 30 second experiments that could've been done on your own.

Brad Wilson (dotnetguy.techieswithcats.com)
Wednesday, January 19, 2005

A nice example for justifying a "finally" block is cleaning up resources. Imagine doing file handling, database manipulation etc. In case something goes wrong you don't expect, you don't want your resources not being cleaned up. In the finally block it's a good idea to close db connections, and other resources when working with them.

Gerrit Quast
Friday, February 04, 2005

*  Recent Topics

*  Fog Creek Home