Solved

How to write a 'catch all' exeption handler

Posted on 1998-02-09
6
1,056 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

24 Experts available now in Live!

Get 1:1 Help Now