On 02/23/2013 02:44 AM, Lubos Pintes wrote:
> Maybe I don't understand this example fully,

It is one way of adding information to an exception. (There are other ways.)

> but where is a "try"
> statement?

It is not needed here because the requirement is achieved by the destructor of LineInfo.

Besides, try-catch should be used only when there is something to do if an operation fails. That's one of the benefits that exceptions bring: mid-layers do not need to propagate error conditions. The code that fails to do something throws and the code that can do anything about it can catch.

If you meant that main() should never leak an exception but instead print an error message to the user, I agree. But this example only demonstrated how a piece of information can be added to an exception in flight.

> The to!int may throw...

Indeed, it does for "hello" and the output includes both the original error and the added information:

std.conv.ConvException@/usr/include/d/dmd/phobos/std/conv.d(1826): Unexpected 'h' when converting from type string to type int
[...]
object.Exception@deneme.d(121171): Line number: 2
[...]

Ali

Reply via email to