Solved

How to write a 'catch all' exeption handler

Posted on 1998-02-09
6
1,055 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now