6

I want to disable the exception catching by Delphi and let Windows catch it - making it produce a window like "AppName crashed. Debug , Send", add this to Application events, create a memory dump and so on.

By default, Delphi catches all the exception in TApplication.Run procedure... How can I avoid that without modifying Forms.pas?

2
  • 6
    I'd strongly recommend against this, and strongly recommend in favor of a tool like MadExcept, or even the exception handler that is part of the JEDI project. Aug 26, 2012 at 23:56
  • It's for debugging purposes. By doing this I want to catch errors in external DLL's used in the project, that's why I want Windows handle things. I do use madExcept for production.
    – djsoft
    Aug 27, 2012 at 11:24

4 Answers 4

8

You could add an OnException handler that re-raised the exception:

class procedure TMainForm.OnException(Sender: TObject; E: Exception);
begin
  raise Exception(AcquireExceptionObject);
end;

initialization
  Application.OnException := TMainForm.OnException;

I'm not sure why you would want to do this at all though. It's more normal to use a tool like madExcept or EurekaLog to show an error dialog that yields much more helpful information than the system dialog.

1
  • This works :) Thanks! madExcept and other tools like this provide useful info when debugging own code, but when it's a bug in the 3rd party DLL it isn't that helpful.
    – djsoft
    Aug 27, 2012 at 11:32
4

You can set JITEnable to '1' or higher (default is '0'). With '1', non native exceptions, with higher than '1', all exceptions will be handled by JIT or WER (depending on the system).

This may not be what you want though. With this solution any qualifying exception will be passed to the OS, it doesn't matter if they're handled in code or not. Clarification (run outside the debugger):

procedure TForm1.Button1Click(Sender: TObject);
begin
  raise EAccessViolation.Create('access denied');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  try
    PInteger(0)^ := 0;
  except
  end;
end;

initialization
  JITEnable := 1;

The first example is a native exception, it will be handled by the application exception handling mechanism when JITEnable is 1. But the second example will trigger JIT/WER.

1
  • 3
    Thanks! JITEnable := 2 seems like what I was looking for.
    – djsoft
    Aug 27, 2012 at 11:36
3

Add your own handler. Application.OnException is probably what you want. Better than leaving it up to windows as well, as you get different behaviours depending on the environment. For instance if windows error reporting is on, it will ask the user if they want to send an error report to MS.

Like Mr Heffernan I recommend you look at something like EurekaLog.

1

AS. I agree with voices above that this wish is rather strange. I also agree that practically hooking in TApplication.OnException would probably be enough ("if it looks like a duck...")

However if you truly want to make RTL oblivious to exceptions, there are ways too.

Exception handlers are plugin to low-level RTL, just like heap management, etc.

You can look at KOL (Key Objects Library). In Delphi 5 times i managed to make 2KB-size DLL.

That required absense of many usualyl taken "for granted" features. Exception were among them.

To enable Exceptions in KOL's system RTL replacement, you had to make some $DEFINE's, and then the code to add exceptions support to IDE was unlocked.

I believe you can still get that modularized RTL version and grep for that $IfDef and see which code is replaced with which. I believe there is fair chance you can undo that and make Windows avoid calling Delphi RTL over Exceptions. I don't remember details, but i believe Delphi RTL Exception handler is just registered in Windows core as a callback. And you probably can de-register it (register nil callback). I believe you can find it in stock RTL, but KOL's modularised RTL would just make it easier to search.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.