Thread overview
Catching OS Exceptions in Windows using LDC
Jul 04, 2020
realhet
Jul 04, 2020
Adam D. Ruppe
Jul 04, 2020
kinke
Jul 04, 2020
Kagamin
Jul 07, 2020
realhet
July 04, 2020
Hi,

I'd like to catch the OS Exceptions including:

- access violation
- int 3
- invalid opcode
etc.

The default behavior is that when these events happen, the program immediately exits with some negative exit code.

It was not a problem on other systems like: MSVC or Delphi, but on LDC these events are completely ignored.

It would be very useful to catch these errors inside and have the opportunity to handle it whatever is the best: application exit, save important data, ignore.

I've already found a solution from Adam D. Ruppe, but that's for Linux for the Access Violation case. But is there a similar thing for Windows as well?

PS: I do believe rely on AccessViolation to avoid buffer overruns is really useless and unsecure. But it would be so much help for me for developing/debugging my programs.

Thank You!
July 04, 2020
On Saturday, 4 July 2020 at 12:45:50 UTC, realhet wrote:
> It was not a problem on other systems like: MSVC or Delphi, but on LDC these events are completely ignored.

use dmd with -m32 or -m32mscoff and it works correctly automatically.

For whatever reason, dmd 64 bit and ldc decided to do their own thing instead of following the Windows standard and thus have no interop with OS exceptions.

Alas not much help if you must use those other builds....
July 04, 2020
try https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-setunhandledexceptionfilter
July 04, 2020
On Saturday, 4 July 2020 at 12:59:03 UTC, Adam D. Ruppe wrote:
> For whatever reason, dmd 64 bit and ldc decided to do their own thing instead of following the Windows standard and thus have no interop with OS exceptions.

For LDC, we don't do 'our own thing', but use MSVC++ EH, which allows to catch MSVC++ exceptions in D, and with some work, D exceptions in C++.
July 07, 2020
On Saturday, 4 July 2020 at 14:44:06 UTC, Kagamin wrote:
> try https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-setunhandledexceptionfilter

Thank You, this was the winner for me.
Not just I can catch the OS Exceptions, I can check and alter the CPU state and decide to continue execution from an optionally modified cpu state, or just stop and show the error.

It needed some work, but this seems the best way for LDC2 Win64.