?
Solved

Handling Exceptions

Posted on 1999-01-23
11
Medium Priority
?
613 Views
Last Modified: 2010-05-19
Hi...
  I'm no expert at writing code at handling exceptions and this is basically my first attempt at it. Of course, I'm stumped right away! My app calls a 3rd party sort routine DLL in the form of an ActiveX control. Here's my code...

try
  Supersort1.Sort;
except
  ckforsorterror;
end;

...Simple enough...I expected that any exception that occurred would cause my handling routine 'ckforsorterror' to be executed, but not so. As I step thru the code and come to Supersort1.sort, it steps into the code unit of the SuperSort ActiveX control module, and while in there generates the EOleException...and the program aborts...never reaching my 'cksorterror' procedure.
  What's happening here? How can I get my 'cksorterror' procedure to handle the exceptions from this sort routine? The author of the sort routine does not know Delphi, but he gave me examples of how to handle the exception in VB and VC++. Here's what he wrote:

I cannot recall the exact Delphi procedure for handling exceptions, but
in Visual Basic you would write something like this:

On Error GoTo Handler

Supersort1.LoadProfile (ProfileName)
Supersort1.Sort

' If everything is OK, simply return from the function
Exit Sub

Handler:
    dim error as Long
    ' Get the last error code from Supersort
    error = Supersort1.GetError

    ' This error indicates an empty input file.
    ' If this is OK, do nothing and return success!
    if error = 20019
    else
    End If

In C++, you would write something like this

try {
   Supersort1.LoadProfile(....
   Supersort1.Sort()
   return SUCCESS;
}
catch(CException* e)
{
     long error = Supersort1.GetError()
     if (error == 20019) return SUCCESS;
     else {
        // Another error was raised, handle it
     }
}
return FAILURE;


...this VC++ code kinda looks like how it would be handled in Delphi...but somethings not working right. Is there some compiler directive that I'm missing somewhere? It's probably something real simple that I'm just overlooking.

Thanks
   Shawn Halfpenny
   drumme59@sprint.ca
0
Comment
Question by:aztec
[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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1362996
here is an example....

var
  a, b, c : integer;
begin
  a := 10;
  b := 0;
  try
    c := a div b;
  except
    on EDivideByZero do
      //whatever
  end
end;

If you need more help let me know....

-Viktor
--Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1362997
or in your case...

try
  Supersort1.Sort;
except
  ShowMessage('Well there was an error. Sorry');
end;

-Viktor
--Ivanov
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1362998
Hi Shawn, I think your code is correct! Perhaps you executed the program inside of Delphi? Delphi stops on always on exceptions. You can turn that off somewhere. But mostly it helps a lot, because so you can find the line, where the exception occured.

So please let the program run outside of Delphi and tell us what happens...

Regards, Madshi.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:viktornet
ID: 1362999
Oh, yeah, Madshi is right..... YOu should compile your program to get an EXE and then run the EXE outside the Delphi IDE and you'll see that it really checks for the errors...

-Viktor
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1363000
I think you guys missed the point. What it boils down to is that the sort routine is probably handling it's own exceptions.
For example:

IN THE DLL-----------------------------
function Sort: Boolean;
begin
  try
    //do some stuff here
    //...
   Result := true;
  except
    on E: Exception do
      ShowMessage(E.Message);
      Result := false;
  end;
end;
//ie if an exception occurs, the function handles it and returns false.

IN YOUR PROGRAM----------------
procedure TForm1.Button1Click...
begin
  try
    Sort;
  except
    ShowMessage('You''ll never get here!');
  end;
end;


Your own exception handler won't process any errors in the Sort routine, because it (may) handle it's own errors.

If you're reading this thinking that I'm totally off track, and you just want exception handling code then the normal constructs are:

try
//Do stuff
except
  //Catch all exceptions
  ShowMessage('An error occurred');
end;

or

try
//Do stuff
except
  on EConvertError do
    ShowMessage('Some sort of conversion error happened');
  on EFOpenError do
    ShowMesssage('Unable to open a file.');
  else
    ShowMessage('Unknown error');
    //in the else part, it is quite common to call raise and let the default
    //handler deal with the exception.
    //There is this old rule that says don't capture exceptions
    //that you don't know anything about.
end;

or

try
//Do stuff
except
  on E: Exception do
    ShowMessage('An unexpected condition occurred.' +
                'Call Microsoft for help, and have your credit card ready!'#13#10 +
                'The error message is:'#13#10 +
                E.Message);
end;

This is how to *handle* exceptions. If you just want to free a bitmap or stringlist or something if an error occurs, then you should probably use try..finally..end instead and let the default error handler deal with the exception. try/finally is much more common in the vcl code than try/except.

Cheers,
Phil.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 1363001
Hi Phil, aztec said, exception handling would work fine with C(++) and VB, so I don't think the sort routine has its own exception handler...

Regards, Madshi.
0
 
LVL 3

Expert Comment

by:philipleighs
ID: 1363002
raise Exception.Create('Oh, OK looks like I didn''t read the question thoroughly enough!');
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1363003
:-)))
0
 

Author Comment

by:aztec
ID: 1363004
Sorry Viktor...Madshi was right...my code was correct! All I had to do was run the program from outside the Delphi environment. Sorry! I guess Madshi should get the points, eh?
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1363005
well, that's obvious... I thought you knew that and that's why i was trying to give you the way you should catch the errors... anyway..let Madshi answer...
0
 
LVL 20

Accepted Solution

by:
Madshi earned 200 total points
ID: 1363006
Thanx, friends...   :-)
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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 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