Error Management under Pascal

October 2016


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.


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; 
f : File; 
name : string; 
     Write ('File Name: '); Readln (name); 
     Assign (f, nom); 
     Reset (f); 
     If IOresult <> 0 then Rewrite (f); 

Thanks to Zouari Lazhar for this tip.

Related :

This document entitled « Error Management under Pascal » from CCM ( is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.