DIP33: A standard exception hierarchy

Ali Çehreli acehreli at yahoo.com
Tue Apr 2 12:10:47 PDT 2013


On 04/02/2013 12:01 PM, Jesse Phillips wrote:> On Monday, 1 April 2013 
at 22:46:49 UTC, Ali Çehreli wrote:
 >> But I also remember that an AssertError may be thrown by an assert()
 >> call, telling us that a programmer put it in there explicitly, meaning
 >> that the program cannot continue. If there was any chance of recovery,
 >> then the programmer could have thrown an Exception or remedy the
 >> situation right then.
 >>
 >> Ali
 >
 > I don't think assert/Error makes any statement on the ability to
 > recover.

Agreed. Error says that the program state is in an unknown state, while 
Exception says that we could not complete our task.

 > What it usually means is you need to fix this because I won't
 > be checking this condition when you throw on that release flag.

Aside: That's why the release flag should be used only when one is sure 
that all possible runtime scenarios are tested. I don't understand how 
anybody can be sure of that though. :)

 > If you are doing input validation you should be throwing an exception.

Agreed.

 > We can still throw exceptions in production, I don't tend to use this,
 > but maybe this would be a time to say "invalid state stop." But then how
 > do you distinguish it from "fix your program?"

(I am not sure that I understand that comment correctly.)

The benefit of exceptions is that a low level function throws an 
exception if it cannot achieve its task. This is very different from 
incorrect program state. Then, a higher level function (usually all the 
way at the user interaction layer), catches this exception and reports 
"I could not do it because of such and such."

The program state is still good because we have cleaned up everything as 
a result of stack unwinding.

 > I've mostly enjoyed having temporary files being cleaned up upon some
 > range access error which has no effect on my removing files that are no
 > longer valid.

The problem is, the runtime cannot know that it will be doing what you 
really wanted. The incorrect program state may result in deleting the 
wrong file.

Ali



More information about the Digitalmars-d mailing list