[OT] OT: Null checks.
Timon Gehr
timon.gehr at gmx.ch
Tue May 6 15:48:18 UTC 2025
On 5/6/25 08:11, Walter Bright wrote:
> D is set up so if you throw an `Exception`, then destructors will run as
> the stack unwinds. But if you throw an `Error`, you can catch it but the
> destructors don't run.
> ...
They do.
```d
import std.stdio;
void main(){
scope(exit) writeln("hi");
assert(0);
}
```
This still runs even if I mark `main` nothrow. I understand it's not
guaranteed (which is bad), but I will take anything I can get.
> The reason is that `Error` means the program has entered an invalid
> state. Nothing in the program can be trusted any more. The program
> should do as little as possible to close down gracefully.
> ...
"The patient has a light cough. The patient has thereby entered an
invalid state. The doctors must now do as little as possible before they
blow up the hospital in order to euthanize the patient and everyone else
that may have been in contact with them."
I want to diagnose and heal the patient!
> The template `std.exception.enforce` works like `assert`, but throws an
> `Exception` instead.
>
> The documentation for `enforce()` says:
>
> $(NOTE `enforce` is used to throw exceptions and is therefore intended to
> aid in error handling. It is $(I not) intended for verifying the logic
> of your program - that is what `assert` is for.)
> ...
Oh I know. I am still going to use `enforce` to verify the logic of my
program. Why? Because the language becomes hostile to your program once
it can tell anything went wrong. I just want to fix or work around the
bug and move on.
> What you can do is override the default assert behavior by inserting
> your own assert handler by calling `core.exception.assertHandler()`. Be
> sure to set `-checkaction=D`
>
> Then you can have assert() behave however you want.
Well, I just don't want any hard crashes in production. Druntime throws
other kinds of errors besides assert errors, by the way.
More information about the Digitalmars-d
mailing list