Problem with finally clause in C#

Started by
22 comments, last by yaroslavd 20 years ago
Turing was a mathematician who amongst other things
- Cracked German Engima codes during WWII
- Helped build several computers during and after WWII
- Put his name on several of the main pieces of computer science, notably the Church-Turing thesis.

Turing-complete means it can do everything which is computable. Computable is something which can be defined mathematically, notably the "halting problem" (for which the thesis is famous) is NOT computable in the general case.

I didn''t realise that in Java you can return inside a finally block. My response to this would be that either:
- It has some application so obscure that it isn''t worth thinking about
or
- It''s a quasi-bug*; don''t do it.

Mark

*A quasi-bug is my name for something which isn''t technically a bug, but was some kind of feature which may have slipped in accidentally, and would have been a bug, except that its behaviour is well defined and possibly even useful (although not usually).
Advertisement
sjelkjd,

After testing (with lots of disbelief) with code similar to yours, it seems that there are no multiple exceptions alive at once. I think the return statement in the finally clause actually "eats" the thrown exception.

public class App{    public static void main(String[] args)    {        int a = 0;                try        {            a = 100 * get() + 10 * get() + get();        }        catch ( Exception e )        {            System.out.println(e.getClass().getName());        }               System.out.println(a);    }        public static int i = 0;        public static int get()    {        ++i;                try        {            switch ( i )            {                case 1:                    throw new IllegalArgumentException();                                    case 2:                    throw new IndexOutOfBoundsException();            }                                  return 1;        }        finally        {            return 2;        }    }}


It just prints 222. Only if I remove "return 2;" from the finally clause the catch is reached.
Also notice that the return in the finally clause overrides the regular return (otherwise the output would be 221).

Java will never cease to amaze me...
(btw, C# forbids return statements in its finally clauses)
quote:Original post by Anonymous Poster I think the return statement in the finally clause actually "eats" the thrown exception.


Weird, very weird...
quote:Java does it.

It''s one of Java''s many misfeatures. It''s fucking idiotic. javac 1.5 will warn about it.

That this:
try{    return 2;}finally{    return 4;} 

returns 4 and not 2 is a combination of crap and stupid that''s rarely encountered.

C# is very much doing the Right Thing by prohibiting this abomination.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement