4

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

Community
  • 1
  • 1
mjn
  • 36,362
  • 28
  • 176
  • 378

2 Answers2

9

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.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • 5
    To add to this, exceptions in Delphi, whether derived from `Exception` or not, ultimately go through an OS API call to actually raise the exception, such as `RaiseException()` on Windows. The RTL has core exception handlers in place to catch just about any type of exception the OS can report. Raised objects are wrapped with a special exception code so those handlers can recognize them and decode the raw exception data back into the original objects when triggering `except` blocks. – Remy Lebeau Dec 14 '12 at 21:22
  • 1
    Hmmm. Hypothetically: What would happen to that uncatchable Exception? Where would it land? Who can take care of it? If it is the OS you want to reach, wouldn't a simple `halt` to the trick? – alzaimar Dec 14 '12 at 22:12
0

Sometimes exceptions raised within a dll and not caught within that dll are not caught by the exception handler of the calling application either. I wonder whether it is possible to mimic that behavior without using a dll?

dummzeuch
  • 10,975
  • 4
  • 51
  • 158
  • The only way to mimic something like that is to use a dll. – Jerry Dodge Dec 16 '12 at 01:03
  • 2
    That's an uncaught exception because of missing exception handling, not an exception that is uncatchable. You can mimic that behavior by disabling exception handling altogether in an application, see JITEnable in help. – Sertac Akyuz Dec 16 '12 at 01:55