On 2010-02-06 08:20:13 -0500, Jacob Carlborg <[email protected]> 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
[email protected]
http://michelf.com/

Reply via email to