Solved

Handling Exceptions

Posted on 1999-01-23
11
601 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
  • 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
 
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
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 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 50 total points
ID: 1363006
Thanx, friends...   :-)
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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
A short film showing how OnPage and Connectwise integration works.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 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

19 Experts available now in Live!

Get 1:1 Help Now