Solved

How to write a 'catch all' exeption handler

Posted on 1998-02-09
6
1,061 Views
Last Modified: 2012-05-04
How can I prevent ALL nasty Delphi expection messages (particularly EAccessViolations) being displayed to users, including during shutdown of my app.  

My Delphi 3 app calls a lot of functions in C++ DLLs involving callbacks and I often get spurious EAccessViolations occuring, particularly during shutdown after I call FreeLibrary.  I suspect I'm getting memory corrputions somewhere and want to be able to handle the errors more gracefully than at present.

I've tried writing a procedure to handle the TApplication.OnException event but exceptions still appear:

procedure TMy.OnApplicationException(Sender: TObject; E: Exception);
begin
  { do nothing, but prevent error message being displayed }
end;

Thanks
0
Comment
Question by:andrewjackson
6 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1358802
Exceptions cannot handle AccessViolations.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1358803
My C experience makes me guess ronit is right, even if the Delphi documentation doesn't say anything about this particular topic.
0
 

Author Comment

by:andrewjackson
ID: 1358804
Thanks for your replies. I take both your points but I'm still surprised by what you say.  

The 'machine code' or whatever it is that the Delphi compiler embeds in the form of an 'exe' file is able to trap this type of exception during run-time because the 'EAccessViolation at ???? etc.' message that appears is very obviously a Borland / Delphi generated message.  Given that Delphi is so object-oriented and provides developers with basically full access to the VCL, WIN32 API etc. I'm puzzled that I can't somehow override the method or whatever it is that Delphi embeds in my 'exe' to trap this error and display that horrible message.

Any nore thoughts?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:ronit051397
ID: 1358805
It catches some AccessViolations but not all of them:

Try this one which does not catch the error:

procedure TForm1.Button1Click(Sender: TObject);
var xxx: pchar;
begin
  try
    strpcopy(xxx, 'test');
  except
  on E: EAccessViolation do
    Showmessage('You violated memory access');
  end;
end;

Now try this one which do catch the error:

procedure TForm1.Button1Click(Sender: TObject);
var xxx: pchar;
begin
  try
    Getmem(xxx,0);
    Strpcopy(xxx, 'test');
    Freemem(xxx);
  except
    on E: EAccessViolation do
    Showmessage('You violated memory access');
  end;
end;

0
 

Accepted Solution

by:
gipa earned 50 total points
ID: 1358806
Try this one.

procedure TfrmMain.AppException(Sender: TObject; E: Exception);
begin
  if (E is EDBEngineError) then begin
    ShowMessage('');
  end else  if (E is EExternalException) then begin
    Abort;
  end else if (E is EAccessViolation) then begin
    Abort;  // you can also use 'Halt' to terminate your appl.
  end else ShowMessage(E.Message);
end;

0
 

Expert Comment

by:gipa
ID: 1358807
Ronit

I tested your examples and they do behave as you say, the first exception handler doesn't get called but the second does work properly.  

I then created a generic exception handling procedure (as per my original post and by modifying gipa's suggestion) and assigned this procedure to the Application.OnException event...

procedure TfrmMain.AppException(Sender: TObject; E: Exception);
begin
   MessageBeep(0);
   ShowMEssage('Unknoen exception occurred);
end;

This procedure does get called even for you first example.  However, the application must have already decided at this point that it is aborting because I hear the message beep (several times) but the message box is not displayed which seems to be the norm when a delphi app is shutting down.

So it looks like I am able to at least trap the two exceptions you've provided this way but not been able to do anything more elegant with them!!

Thanks everbody for you help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question