Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to write a 'catch all' exeption handler

Posted on 1998-02-09
6
Medium Priority
?
1,070 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

877 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