Error Management under Pascal

March 2017



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.

Related


Published by jad05.
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 (ccm.net).