collectExceptionMsg and emptyExceptionMsg

Jonathan M Davis jmdavisProg at gmx.com
Wed Apr 18 10:16:52 PDT 2012


On Wednesday, April 18, 2012 17:49:55 Andrej Mitrovic wrote:
> Why does collectExceptionMsg return a special 'emptyExceptionMsg'
> string if the exception caught has no message?
> 
> string collectExceptionMsg(T = Exception, E)(lazy E expression)
> {
> try
> {
> expression();
> 
> return cast(string)null;
> }
> catch(T e)
> return e.msg.empty ? emptyExceptionMsg : e.msg;
> }
> enum emptyExceptionMsg = "<Empty Exception Message>";
> 
> I don't see the purpose of this, why not just return an empty string
> literal "" which won't be null?

Probably to avoid any issues of null vs empty. It also makes it very clear 
that you got an empty exception message when printing the message, whereas 
there won't be any difference between null and empty when you do something like

writeln(collectExceptionMessage(foo()));

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list