0
Thanks

A few words of thanks would be greatly appreciated.

Error Management under Pascal



Issue




By default, in Pascal, error handling is provided by the compiler. For this reason, the program ends by displaying a message starting with Runtime error followed by error number ...

So if the programmer wants to handle the errors itself it must disable the option for error checking by the compiler, using the Option menu and the Build ... or to use the compiler directives directly in the code source program.

For example, if in a given program, we want to open a file by the reset and that the file does not exist, an error message is displayed and the program is stopped.

Solution


To avoid this, we can test whether or not a file and then act accordingly by using the compiler directive $ I and IOresult as the function that wil return the result of entry / exit. With the directive ($ I-), the programmer must manage itself this type of error.


This is a program that lets you enter the physical name of a file and open it. If the file does not exist it is created without ending the program on the error: runtime error 2 at ... (no file).

Opening Program; 
Uses WinCrt; 
Type 
    File; 
Var 
f : File; 
name : string; 
Begin 
     Write ('File Name: '); Readln (name); 
     Assign (f, nom); 
     {$I-} 
     Reset (f); 
     If IOresult <> 0 then Rewrite (f); 
     {$I+} 
end. 


Thanks to Zouari Lazhar for this tip.
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team

Published by . Latest update on by jak58.

This document, titled "Error Management under Pascal," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (https://ccm.net/).

0 Comments