catch without block/scope brackets
Jakob
a at b.com
Tue Sep 4 07:55:46 PDT 2007
Jarrett Billingsley schrieb:
> "Jakob" <a at b.com> wrote in message news:fbjmcu$68k$2 at digitalmars.com...
>> IMHO, the compiler should parse the second piece of code in the same
>> manner as the first piece, since there are (as in the first example) no
>> brackets after the catch keyword defining an object to catch. Right?
>>
>
> I've thought about this as well, and I think it's because allowing a
> statement without braces there would introduce an ambiguity:
>
> try
> foo();
> catch
> (bar).baz();
>
> Parenthesized expressions are allowed to come at the beginning of
> expressions, and therefore at the beginning of statements. So the compiler
> would parse the catch block in this case as "catch(bar", thinking that "bar"
> is the type of a catch specialization, and then expect an identifier after
> "bar".
Well, that makes sense.
But i also tried
catch()
foo();
and it errored, even though this wouldn't be ambigous
>
> Thankfully there's no ambiguity when you _do_ specify an exception
> specialization:
>
> try
> foo();
> catch(Object ex)
> fwritefln(stderr, "An error happened. %s", ex.toString());
>
>
Yeah, but i didn't use the exception-object and thought, it's stupid to declare a variable and then not use it.
More information about the Digitalmars-d
mailing list