Adding more information to exceptions

Ali Çehreli acehreli at yahoo.com
Sat Feb 23 14:11:31 PST 2013


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 at deneme.d(121171): Line number: 2
[...]

Ali



More information about the Digitalmars-d-learn mailing list