Function with try/catch and no return statement

retard re at tard.com.invalid
Sat Feb 6 07:01:14 PST 2010


Sat, 06 Feb 2010 09:42:37 -0500, Michel Fortin wrote:

> 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.

The difference is, in this case the generic foo doesn't need to be 
analyzed before it has been instantiated. There is no foo(string code) 
instances in the final executable, only the concrete instantiations. The 
analysis could be done when foo!(something) is encountered in the code.



More information about the Digitalmars-d mailing list