Global Error Trapping

Blitz3D Forums/Blitz3D Beginners Area/Global Error Trapping

RustyKristi(Posted 2016) [#1]
I was wondering instead of different messages or MAV just showing up, you can create all fatal errors or mavs with only one message or a global error message handler.

Maybe something similar like in PHP command here..

http://php.net/manual/en/function.set-exception-handler.php

or the ON ERROR GOTO like in visual basic/powerbasic

http://www.powerbasic.com/help/pbcc/error_trapping.htm
https://msdn.microsoft.com/en-us/library/5hsw66as.aspx?f=255&MSPPError=-2147217396

maybe in combination with RuntimeError?


jfk EO-11110(Posted 2016) [#2]
Hehe, ON ERROR GOTO... I missed that one too for a while.

But believe me, the lack of such a feature will teach you to write stable, clean Programs.

Tho, there is the bufferdirty group of commands that allow to reinit the gfx in case of memory loss due to eg. Screensaver, Hibernation/Standbye etc.

Also, whenever you tell Blitz to create a handle, eg. by loading an image, mesh, sound or opening a filehandle etc. you should check this handle whether it is <>zero.


RustyKristi(Posted 2016) [#3]
thanks jfk. But this is more like an end user thing. I can see there's none so maybe I'd stick to zero handle check.


Yasha(Posted 2016) [#4]
A MAV is Blitz3D's terminology for a segfault (I don't know how nonstandard "MAV" is, but certainly Blitz3D, or games made in it, top the Google results).

You can usually catch a segfault by installing a signal handler for SIGSEGV. This is presumably what B3D already does in order to be able to give the error message rather than allowing the program to simply die.

You might be able to combine this with either setjmp or C++ exceptions to create an ad-hoc exception handling system, but it would always involve jumping "out" to a less-deeply nested part of the program. You can't really fix the error in-place and continue, because you'd need to detect exactly what variable had the invalid address and fix it up with valid memory to work with... if you can manage that kind of deep wizardry, it should be well within your ability to write a program that doesn't have simple mistakes in the first place. (An unexpected segfault is always a programmer error. You really shouldn't be catching them, you should be fixing them.)

I tried building something like this a while back: Minimalist exceptions

I didn't experiment with installing signal handlers (SIGSEGV for MAV and presumably stack overflow, SIGFPE looks like the one for integer division errors), but the same principle would apply.

It has the major disadvantage that using a blind jump up the call stack will bypass any cleanup instructions that are supposed to run at the end of a function. Blitz3D uses simple reference counting for Type instances (and I think for strings), which means that in practice this code is likely to leak memory like a sieve.

There is definitely no way to do this without userlibs. Blitz3D already doesn't support function pointers, meaning that you need outside-the-language hackery just to get as far as putting an "action" (function pointer) into a variable in order to use it as a value.


RustyKristi(Posted 2016) [#5]
Thanks Yasha! This is something really related to what I'm looking, will check it out asap.