Exception: constructor arguments?

Jonathan M Davis jmdavisProg at gmx.com
Sun Aug 14 11:38:09 PDT 2011


On Sunday, August 14, 2011 15:48:36 mimocrocodil wrote:
> Johannes Pfau: thanks!
> 
> Next question:
> 
> Why do we always have to send a text message with a standard class of
> Exception?
> 
> After all, it might be never used, for example due to the fact that error
> messages are not sent to the user "as is": exception can be analyzed and
> translated for internationalization, for example.
> 
> Is it right way that the standard library forces user to store error msg
> and, in general, leads to inefficient memory usage. (No, I'm not kidding!
> I'm worried about the design. :) )

Then give null for the message value. Usually, it's a _bad_ thing not to have 
a message, because it makes it harder to figuree out what happened. So, from 
the standpoint of Exception actually working well as an error-handling 
mechanism, it's better to require a message rather than defaulting to none. 
Also, on some level, _it doesn't matter if Exception has inefficient memory 
usage_. Exceptions are _expected_ to be slower than normal code. They are used 
when something goes wrong. They are _way_ slower than normal code - so much so 
that a string is going to have pretty much zero effect on efficiency one way or 
the other. It would be nice if they were made to be more efficient so that unit 
tests weren't so slow when you checked to make sure that the proper exception 
is thrown when it's supposed to for a function, but for normal code, it 
doesn't really matter much. It would be nice if they were faster, but since 
they're supposed to be exceptional, it's not all that big a deal. Exceptions 
are slow in every language that I've ever used. Now, they're a lot faster in 
Java than D, and as I said, it would be nice if they were faster for at least 
unit testing purposes, but expect exceptions to be slow. Efficiency is _not_ a 
primary concern of exceptions.

So, if you really want to avoid the memory allocation for the message, just 
pass it null, but don't expect it to have any real impact on the efficiency of 
exceptions. Exceptions are slow by their very nature. They could be faster 
than they are, but no matter what we do, the plumbing required to make 
exceptions work makes them far slower than normal code.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list