TURBO PASCAL |
Новости
|
25: 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 e.g. 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. : Actually, I utilize this idea in my /pc/ts/tspa3470.zip Turbo Pascal units collection, which includes a TSERR.TPU. If you put TSERR in your program's uses statement, all the run time errors will be given verbally besides the usual, cryptic error number. That's all there is to it. That is, the inclusion to the uses statement to your main program (if you have the program in several units) is all you have to do to enable this handy feature. : Hans.Siemons@f149.n512.z2.fidonet.org notes "This line: ExitProc := OldExitProcAddress; should IMHO never be placed at the end of your exit handler. If for one reason or another your own handler should cause a runtime error, it would go in an endless loop. If the first statement restores the exit chain, this can never happen. I do agree that is not very likely that your exit handler produces any runtime error, but it performs I/O, and since it is located in a FAQ, people are bound to use, and maybe extend it with more tricky stuff." |
(с)Все права защищены По всем интересующим вопросам прошу писать на электронный адрес |