Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] [OT] C# question -- try / catch / finally



> 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

Home Page Mailing List Linux and Japan TLUG Members Links