?
Solved

Handling Exceptions

Posted on 1999-01-23
11
Medium Priority
?
620 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
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.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 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