[comp.lang.pascal] HANDLING FLOATING POINT OVERFLOW ERROR IN TP

zhou@brazil.psych.purdue.edu (Albert Zhou) (01/01/91)

	Whenever a floating point overflow error is encountered, the program
will unconditionally stop in Turbo Pascal. Is there a way of deactivating it
so as to let the programmer handle this type of error?          
	As I understand, there is no compiler derective that does it. Just
like {$I-} that enables the programmer to handle I/O errors.
	Any clue will be greatly appreciated.

dslg0849@uxa.cso.uiuc.edu (Daniel S. Lewart) (01/01/91)

zhou@brazil.psych.purdue.edu (Albert Zhou) writes:

>       Whenever a floating point overflow error is encountered, the program
> will unconditionally stop in Turbo Pascal. Is there a way of deactivating it
> so as to let the programmer handle this type of error?
>       As I understand, there is no compiler derective that does it. Just
> like {$I-} that enables the programmer to handle I/O errors.
>       Any clue will be greatly appreciated.

Yes, you can point the ExitProc pointer variable to your exit procedure.
It is discussed in the manual.

Daniel Lewart
d-lewart@uiuc.edu

zhou@brazil.psych.purdue.edu (Albert Zhou) (01/01/91)

In article <1991Jan1.033432.11437@ux1.cso.uiuc.edu> dslg0849@uxa.cso.uiuc.edu (Daniel S. Lewart) writes:
>
>Yes, you can point the ExitProc pointer variable to your exit procedure.
>It is discussed in the manual.
>
It didn't turn out to be working well. Following is the code that gets stuck
after executing my own error processing subroutines:

procedure error;
begin
  writeln('Error!');
  (***** gets stuck here ******)
end;
const x : real = 3000;
begin
  exitproc := @error;
  x := exp(x);
  (**** exp(3000) should result in overflow ****)
end.

guillory@storm.dnet.nasa.gov (Anthony R. Guillory (904)) (01/02/91)

You need to compile your "procedure error" with {$F+}, i.e,
{$F+} Procedure Error; {$F-}
[your code]
You should also save the address of TP original exitProc by using
SaveExitProc := exitproc;
exitproc := @error;
Then at the end of your "procedure error" set exitproc := SaveExitProc
This is all documented in the manual, thelp, and most 3rd party books.

Anthony
Guillory@storm.dnet.nasa.gov

zhou@brazil.psych.purdue.edu (Albert Zhou) (01/03/91)

In article <25363@adm.brl.mil> guillory@storm.dnet.nasa.gov (Anthony R. Guillory (904)) writes:
>You need to compile your "procedure error" with {$F+}, i.e,
>{$F+} Procedure Error; {$F-}
If I set {$F-} then the TP error will not be generated, but the program gets
stuck. However, if I set {$F+}, the procedure error will be executed normally,
but a TP error will still be generated. It's really weird.
It happens to me once in a while when everything looks right and the program
doesn't do what it's supposed to do.

guillory@storm.dnet.nasa.gov (Anthony R. Guillory (904)) (01/03/91)

Here's a small excerpt from an run-time error handling routine I wrote. 
I don't know if this will help, but here it is.


{$F+} Procedure RuntimeErrorHandler; {$F-}

Begin

If (ExitCode <> 0) AND (ErrorAddr <> NIL)
Then Begin
 
        [Print "My" Error Message]

        ErrorAddr := NIL; { Cancel Runtime Error }
        ExitCode := 0;
     End { Then };

ExitProc := SavedExitProc;

End { RuntimeErrorHandler };

Begin

   SavedExitProc := ExitProc;      { Save ExitProc pointer }
   ExitProc := @RuntimeErrorHandler;

End. { ExitProc }




_______________________________________________________________________________
 
Anthony R. Guillory
 
INTERNET:    Guillory@Storm.dnet.nasa.gov
UUCP:        {ames,east,decwrl,ucbvax,uunet}!storm.dnet.nasa.gov!guillory
CompuServe:  73427,2515
GEnie:       A.GUILLORY
FidoNet:     Anthony Guillory @ 1:3605/46
DECNET/SPAN: STORM::GUILLORY  or  723::GUILLORY

 o If practice makes perfect, and nobody's perfect, why practice?
 o The solution of this problem is trival and is left as an exercise for
   the reader.
 o Recursive,adj.; see recursive.
_______________________________________________________________________________

ts@uwasa.fi (Timo Salmi) (01/04/91)

In article <11637@j.cc.purdue.edu> zhou@brazil.psych.purdue.edu (Albert Zhou) writes:
>
>	Whenever a floating point overflow error is encountered, the program
>will unconditionally stop in Turbo Pascal. Is there a way of deactivating it
>so as to let the programmer handle this type of error?          
>	As I understand, there is no compiler derective that does it. Just
>like {$I-} that enables the programmer to handle I/O errors.
>	Any clue will be greatly appreciated.

25. ***** 
  Q: How can I trap a runtime error?

  A: What you are probably asking for is a method writing a program
termination routine of your own. To do this, you have to replace
Turbo Pascal's ExitProc with your own customized exec procedure.
Several Turbo Pascal text books show ho to do this. See eg Tom Swan
(1989), Mastering Turbo Pascal 5.5, Third edition, Hayden Books, pp.
440-454; Michael Yester (1989), Using Turbo Pascal, Que, pp.
376-382; Stephen O'Brien (1988), Turbo Pascal, Advanced Programmer's
Guide, pp. 28-30; Tom Rugg & Phil Feldman (1989), Turbo Pascal
Programmer's Toolkit, Que, pp. 510-515. Here is an example
  var OldExitProcAddress : Pointer;
      x : real;
  {$F+} procedure MyExitProcedure; {$F-}
  begin
    if ErrorAddr <> nil then
      begin
        writeln ('Runtime error number ', ExitCode, ' has occurred');
        writeln ('The error address in decimal is ',
                  Seg(ErrorAddr^):5,':',Ofs(ErrorAddr^):5);
        writeln ('That''s all folks, bye bye');
        ErrorAddr := nil;
        ExitCode  := 0;
      end;
    {... Restore the pointer to the original exit procedure ...}
    ExitProc := OldExitProcAddress;
  end;  (* MyExitProcedure *)
  (* Main *)
  begin
    OldExitProcAddress := ExitProc;
    ExitProc := @MyExitProcedure;
    x := 7.0; writeln (1.0/x);
    x := 0.0; writeln (1.0/x);   {The trap}
    x := 7.0; writeln (4.0/x);   {We won't get this far}
  end.

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun