分享

delphi

 quasiceo 2014-07-08

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?

asked Aug 26 '12 at 17:47
djsoft
29318

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. –  Nick Hodges Aug 26 '12 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 '12 at 11:24
add comment

4 Answers

up vote 4 down vote accepted

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.

answered Aug 26 '12 at 22:53
Sertac Akyuz
32.4k34173

3  
Thanks! JITEnable := 2 seems like what I was looking for. –  djsoft Aug 27 '12 at 11:36
add comment

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.

answered Aug 26 '12 at 18:08
David Heffernan
287k20354619

    
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 '12 at 11:32
add comment

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.

answered Aug 26 '12 at 18:23
Tony Hopkinson
15k21225

add comment

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.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多