Seven.FFF wrote: ↑Tue Oct 17, 2023 3:47 pm
Yes, structured error handling is a common and great use of the technique. In my esXDOS dot commands errors can happen any time due to external causes (serial timeouts, external hardware misbehaving) and dozens of stack levels deep, so it ends up much more robust being able to jump out and rebalance the stack from any of those arbitary points, instead of having to back out from the error one level at a time.
My first published game (PS1) had to use structured exception handling, mainly because of my n00b design
I was reading the joypad all over the place in the code (instead of just reading it once in the VBlank and remembering what was pressed - n00b mistake).
Then Sony announced that if you hold down Start and Select buttons for 2 seconds your game must return to the title menu or you would fail submission... oops.
So I basically did a GOTO MAINMENUINIT inside the pad reading routine if it detected that lol.
(But it was in C, so it was setjmp() and longjmp() which is how structured exception handling is done in C). C++ might do something similar at a very low level but it can't just set the stack pointer and jump to an arbitrary place in the code it has to make sure all destructors are called, so it has to do stack unwinding).
I agree structured exception handling is an excellent way to deal with errors (even in C++ if it's in a place where performance isn't that important).
Classic example is parsing a file, lots of errors can occur due to file read errors, parse errors, etc. Instead of putting error checks in everywhere it's far easier to do this:
Code: Select all
ParseFile()
{
try
{
// do stuff to parse the file, assume everything is A-OK at all times
// e.g.
versionNum = ReadInt(); if it isn't an int, this will throw ParseError. If there's a file read error, it will throw FileSysError
}
catch(FileSysError fserr)
{
// deal with the file system error
}
catch(ParseError parserr)
{
// deal with the parse error
}
}
Not the most efficient way probably but certainly the easiest. Linus probably wouldn't like it