Solved

How to write a 'catch all' exeption handler

Posted on 1998-02-09
6
1,066 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

615 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