Solved

Floating Point division by zero exception handling in DLL's (Delphi 1.0)

Posted on 1997-03-16
7
1,833 Views
Last Modified: 2008-03-10
I am having serious problems trapping the floating point
division by zero exception within DLL's in Delphi 1.0.

It seems to work fine in Delphi 2.0.
It also works fine within the program section of Delphi 1.0.

Here is some stripped down code:
************************************************************
Code to Call Form from Application in DLL.

Type
  Proc = procedure;

procedure THelpaMainForm.Button1Click(Sender: TObject);
var
  HInst                             : THandle;
  FPointer                          : TFarProc;
  MyProc                            : Proc;
  DLLLoad                           : boolean;

begin
  if HInst > 32 then
    begin
    FPointer := GetProcAddress(HInst,'CreateTemp');
    if FPointer <> nil then
      begin
      MyProc := Proc(FPointer);
      MyProc;
      DLLLoad := true;
      end;
    end;
end;

************************************************************
Code to Create form in DLL

Procedure CreateTemp;
begin
  Screen.Cursor := crHourglass;
  Form1 := TForm1.Create( application );
  Screen.Cursor := crDefault;

  Form1.Show;
end;

************************************************************
Code used to Trap Exception in DLL

procedure TForm1.Button1Click(Sender: TObject);
var
  TESTNUM : real;

begin
  try
    TESTNUM := strtofloat(edit1.text) /                strtofloat(edit2.text);
  except
    on EZeroDivide do
      begin
      MessageDlg('An exception occured!!',mtERROR, [mbOK], 0);
      exit;
      end;
  end;
end;

************************************************************
0
Comment
Question by:boabyte
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:sperling
ID: 1334840
Are you sure it is only the divide by zero that you can't trap? Try explicitly raising a EZeroDivide or an ESomething, and then catch them.

Erik.

0
 

Author Comment

by:boabyte
ID: 1334841
All other excpetions can be trapped.
0
 
LVL 3

Accepted Solution

by:
mheacock earned 100 total points
ID: 1334842
Recently I read about this...I would have been stumped for awhile too.  But now...this one is  almost EASY!  There is one IFFY part...but I'll get to that.

In your Button1Click procedure, I believe your exception is not being thrown because the compiler is optimzing the entire TESTNUM variable away.  It is not being used for anything useful in the procedure, therefore Delphi decides that it isn't needed and optimizes it away.

The IFFY part is the fact that you have a MessageDlg in the except part of the code...would Delphi optimize that away? I'm not sure.  But my thoughts are that it would.  Since MessageDlg is not actually altering any variables in your code, Delphi again views it as garbage code and optimizes it away.

There's your answer.  Make TESTNUM do something useful...perhaps assign it value to a global (after the try..except), then the code will not be optimized away.
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.

 

Author Comment

by:boabyte
ID: 1334843
Unfortunately, this is not the case.  The code supplied is
stripped down to a bare minimum.  The real code uses many floating point type variables and also the expcept part of the
procedure is a little more complicated than just calling a message dialog.  Also any other exception can be trapped (e.g. EDivByZero and various others) I have increased the points for
the question hoping that it could be answered.  If any more info
is required,
just ask.
 
0
 
LVL 3

Expert Comment

by:mheacock
ID: 1334844
In a/b, have you tried explicitly setting b=0??

With floating point

       b = (1.2345 - 0.1234)
       b = b - 1.1111
may not always yield b=0.  The numbers I've supplied are made
up, but you probably get the point.

I'm sure you have tried explicitly setting b=0, but I want to
make sure.
0
 

Author Comment

by:boabyte
ID: 1334845
Yes, I have tried explicitly setting b=0.
The problem is not that the DLL skips the exception,
but the Windows Operating system produces its own error screen
and completely crashes my program.
0
 
LVL 3

Expert Comment

by:mheacock
ID: 1334846
It could very well be the fact that you are calling the exception from code that belongs to this dynamic form you are creating in the DLL.

Will the DLL properly catch your EZeroDivide exception if it not a part of this dynamic form?

Your problem could stem from an inproperly created form within the DLL.  See if you can catch the exception in the manner that you'd like with a DLL call that has nothing to do with the dynamic form.  If everything is all right at that point, then I'd suspect that the dynamic form is the 'real' culprit.
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

Suggested Solutions

Title # Comments Views Activity
proper way to parse text with delphi 7 101
Can Viruses spread while transferring Binary data with Winsock 2 66
find a node in VST 2 64
Run video youtube webbrowse 10 58
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

24 Experts available now in Live!

Get 1:1 Help Now