
Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [tlug] [OT] C# question -- try / catch / finally
- Date: Fri, 22 Nov 2013 08:03:37 +0900
- From: Darren Cook <darren@example.com>
- Subject: Re: [tlug] [OT] C# question -- try / catch / finally
- References: <528E3A9C.7080606@bonivet.net>
- User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130330 Thunderbird/17.0.5
> try {
> doSomething();
> doSomethingElse();
> areWeDoneAlready();
> }
> catch (SomeKindOfException) {
> kaboom();
> }
> finally {
> cleanup();
> }
Doing it that way is nice and clean and clear: people don't *expect* a
catch() clause to fall through and carry on executing in the same function.
finally is even more useful in situations where the exception is being
caught higher up. You can do something like this:
try{
createTempFile();
otherStuff();
}
finally{
removeTempFile();
}
Without finally you have to do:
try{
createTempFile();
otherStuff();
}
catch(Exception e){
removeTempFile();
throw e;
}
removeTempFile();
(In C++ you use the RAII idiom to get around the lack of a finally
statement. In PHP they finally gave in and added finally from PHP 5.5.)
Darren
Home |
Main Index |
Thread Index