catch without block/scope brackets
Jarrett Billingsley
kb3ctd2 at yahoo.com
Tue Sep 4 07:02:18 PDT 2007
"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".
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());
More information about the Digitalmars-d
mailing list