Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I throw an exception that Delphi cannot catch?

Is it possible to construct a snippet of code in Delphi that would make a hypothetical EChuckNorrisException uncatchable?

For the Java programming language I just found this has been answered with Yes in Uncatchable ChuckNorrisException

like image 227
mjn Avatar asked Jan 15 '23 18:01

mjn


1 Answers

No. In Delphi, it's possible to raise any object (TObject descendant,) though by convention this is usually limited to objects that descend from the base Exception class. And it's possible to create a blanket exception handler that will catch anything.

Most catchall exception handlers that try to report information in some way look like this:

try
...
except
  on E: Exception do
    ...
end;

So if you raise something that does not descend from Exception, it will go through this style without getting caught.

However, it's also possible to write it like this:

try
...
except
  ...
end;

Nothing will get by that style of exception handler.

If you raise an exception that is not caught anywhere, the program will immediately terminate with an error. If that's the intended effect, it's possible to do the same thing by calling Halt with a nonzero error code.

like image 126
Mason Wheeler Avatar answered Jan 23 '23 14:01

Mason Wheeler