Mailing List Archive


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

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



On Thu, 21 Nov 2013 16:53:48 +0000, Godwin Stewart <gstewart@example.com> wrote:

> ... C# ...

> try {
>   doSomething();
>   doSomethingElse();
>   areWeDoneAlready();
> }
> catch (SomeKindOfException) {
>   kaboom();
> }
> finally {
>   cleanup();
> }

> Surely this is functionally equivalent:
> 
> try {
>   doSomething();
>   doSomethingElse();
>   areWeDoneAlready();
> }
> catch (SomeKindOfException) {
>   kaboom();
> }
> cleanup();

Well maybe not. 

A web search for C# try finally led to a page by C#'s sponsor 
which seemed to indicate that the finally is executed even if 
a break, continue, goto, or return happens in the try section. 

This expands on what Benjamin Tayehanpour wrote. 
I don't know C# syntax, so the following has speculative syntax. 

while (true) {
    try {
        if (doSomething()) {
            break;
        }
        if (doSomethingElse()) {
            continue;
        }
        if (areWeDoneAlready()) {
            return;
        }
        if (toTheMoonAlice()) {
            goto TheMoon
        }
    }
    catch (SomeKindOfException) {
        kaboom();
    }
    finally {
        cleanup(); # Will this happen even if break, continue, return, or goto happens?
    }
    TheMoon: ;
}



Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links