Benchmark of try/catch

Christopher Wright dhasenan at gmail.com
Mon Mar 23 15:59:26 PDT 2009


dsimcha wrote:
> In your string->int conversion example, it's
> perfectly reasonable that the caller of int() might have no idea what a reasonable
> course of action would be if the conversion fails and would want to pass the buck
> to its caller.  Therefore, exceptions are a perfectly reasonable way to handle this.

C# provides things like int.Parse which throws on an invalid input and 
int.TryParse which takes an integer as an out parameter and returns true 
if the value was successfully parsed.

It's slightly ugly to use TryParse, but most of the time you'll either 
provide a higher level handling mechanism (and so use int.Parse) or be 
dealing with sanitized input (and so use int.Parse).

> One thing that would be nice is if ddoc could automatically document what
> exceptions every function throws when generating documentation.  Of course, there
> would be the problem of dealing with libraries for which the source code isn't
> available, but in a lot of cases, this would be feasible and very useful.

This could be done pretty simply, too -- use the type of the expression 
given with any "throw" statement.



More information about the Digitalmars-d mailing list