Function with try/catch and no return statement
Michel Fortin
michel.fortin at michelf.com
Sat Feb 6 06:42:37 PST 2010
On 2010-02-06 08:20:13 -0500, Jacob Carlborg <doob at me.com> said:
> The following code does not cause a compile error:
>
> int foo ()
> {
> try
> int i;
>
> catch (Exception)
> throw new Exception("");
> }
>
> Wouldn't it be quite obvious for the compiler to see that there is no
> return statement in the above function and give a compile error? The
> same also happens with scope(failure).
I'd say it's as it should. Even though in this particular situation
there is no way the catch block will be used, it's needed for generic
programming. Consider this:
int foo(string code)()
{
try
mixin(code);
catch (Exception)
throw new Exception("");
}
foo!"int i;"(); // same as your example
foo!"writeln(1);"(); // this one might throw
It'd be quite ridiculous if the compiler refused to instantiate the
first only because it cannot throw. The same rules apply inside a
regular function.
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list