[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Handling Exceptions

Posted on 1999-01-23
11
Medium Priority
?
618 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
Industry Leaders: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…
Suggested Courses

650 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