Forum > General

Who catches the Linux signals?

(1/4) > >>

jollytall:
I have a large program and in order to gracefully quit should any signal received I set up a couple of signal catchers, like
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fpsignal(SIGFPE, SignalHandler(@signal_callback_handler));
This takes care of any unexpected error. However at certain places I have divisions. It would be easy to check the divider before the division not to be zero, but in order to take also care should a very large number get divided by a very small number, I rather put it in a try-except:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---try  a := b/c;except  a := 0;  end;
Now the question is should the signalcatcher catch the FPE in this case or the try-except takes care and it does not reach the signal catcher at all?

MarkMLl:
Keep the signal handler simple, and use it to set a flag which is checked every time a timer etc. runs.

Assume that the OS has absolutely no knowledge of the program's state when it sends a signal, and that the heap etc. might be in an inconsistent state.

MarkMLl

Thaddy:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---try  a := b/c;except  on E:EdivByZero do    a := 0  else raise;end;This allows the "expected" exception to be eaten, but the unexpected exceptions are re-raised so you can debug them.
I am not a fan of eating exceptions, but at least done like so it is less harmful.

Zvoni:

--- Quote from: Thaddy on December 09, 2024, 10:15:00 am ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---try  a := b/c;except  on E:EdivByZero do    a := 0  else raise;end;This allows the "expected" exception to be eaten, but the unexpected exceptions are re-raised so you can debug them.
I am not a fan of eating exceptions, but at least done like so it is less harmful.

--- End quote ---
TS doesn't have a Problem with DivByZero (he did write, he can check if c<>0).
His Problem is more how to catch, if b is really big, and c is really small, causing a to "overflow"

jollytall:
To be precise the real question is whether a try-except structure catches first the error and (unless re-raised as Thaddy suggests) then it never reaches the signal catcher OR the signalcatcher is somewhere deep in the operating system and even if the error is in a try-except, the signalcatcher catches it first. In this latter case it is also a question what should or should not the signalcatcher do in order that the error reaches or not the except in the try-except structure. I am interested in the actual way it is written.

Navigation

[0] Message Index

[#] Next page

Go to full version