Difference between revisions of "runtime error"

From Free Pascal wiki
Jump to navigationJump to search
(Created page with "{{runtime error}} A runtime error that occurs during the execution of a program. In contrast, compile-time errors occur while a program is...")
 
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{runtime error}}
 
{{runtime error}}
  
 +
A run-time error is an irreparable error condition that arises during the [[runtime|run-time]], i.e. the execution of a [[Executable program|program]].
  
A runtime error that occurs during the [[runtime|execution]] of a [[Executable program|program]]. In contrast, compile-time errors occur while a program is being [[Compile_time|compiled]].
+
== Behavior ==
 +
The [[FPC]] inserts code to detect a vast number of error situations.
 +
If such a situation is encountered, the standard [[RTL|run-time library]] will initiate the termination of the program.
 +
A run-time error number, and the address the error occurred at is being printed.
 +
This is the safest and cheapest error treatment.
 +
 
 +
== Comparative remarks ==
 +
 
 +
=== Compile-time errors ===
 +
 
 +
In contrast to [[compile-time error|compile-time errors]], which the compiler detects during [[Compile time|compilation]], run-time errors depend on the state of the program, thus can not be foreseen in advance.
 +
If a compile-time error is encountered, no executable program is generated.
 +
 
 +
=== Exceptions ===
 +
 
 +
Run-time errors are the classical imperative approach in order to avoid inconsistent program states, which may eventually cause faulty program behavior.
 +
If FPC's {{Doc|package=RTL|unit=sysutils|text=<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit}} is included, all run-time errors become [[Exceptions|exceptions]] (cf. {{Doc|package=RTL|unit=system|identifier=runtimeerrors|text=<syntaxhighlight lang="pascal" inline>system.runTimeErrors</syntaxhighlight>}} for details).
 +
Unlike run-time errors those can be caught by [[Try|<syntaxhighlight lang="pascal" inline>try</syntaxhighlight>]]...[[Except| <syntaxhighlight lang="pascal" inline>except</syntaxhighlight>]]
 +
[[On|<syntaxhighlight lang="pascal" inline>on</syntaxhighlight>]]...[[Do|<syntaxhighlight lang="pascal" inline>do</syntaxhighlight>]] ... [[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]] [[Block|blocks]], provided a mode allowing exceptions – such as [[Mode ObjFPC|<syntaxhighlight lang="pascal" inline>{$mode ObjFPC}</syntaxhighlight>]] or [[Mode Delphi|<syntaxhighlight lang="pascal" inline>{$mode Delphi}</syntaxhighlight>]] – is being used.
 +
A run-time error causes the program to terminate, while an exception may give the opportunity to “fix” the problem.
 +
This standard behavior of {{Doc|package=RTL|unit=system|identifier=runerror|text=<syntaxhighlight lang="pascal" inline>system.runError</syntaxhighlight>}} can be altered by assigning a non-[[Nil|<syntaxhighlight lang="pascal" inline>nil</syntaxhighlight>]] value to {{Doc|package=RTL|unit=system|identifier=errorproc|text=<syntaxhighlight lang="pascal" inline>system.errorProc</syntaxhighlight>}}.
  
 
== See also ==
 
== See also ==
  
* [[Exceptions]]
+
* [https://www.freepascal.org/docs-html/user/userap4.html Appendix D in the Free Pascal User's Guide: “Run-time errors”]
* [https://www.freepascal.org/docs-html/user/userap4.html Run-time errors]
+
* [[RunError|procedure <syntaxhighlight lang="pascal" inline>runError</syntaxhighlight>]]
* [https://www.freepascal.org/docs-html/rtl/system/runtimeerrors.html Run-Time Error behaviour]
+
* [https://www.freepascal.org/docs-html/current/user/userse58.html “Line numbers in run-time error backtraces”] in the Free Pascal User's Guide
* Procedure [[RunError]].
+
* {{Doc|package=RTL|unit=system|identifier=returnnilifgrowheapfails|text=<syntaxhighlight lang="pascal" inline>system.returnNilIfGrowHeapFails</syntaxhighlight>}}
* [https://www.freepascal.org/docs-html/rtl/system/runerror.html Generate a run-time error].
 
* [https://www.freepascal.org/docs-html/current/user/userse58.html Line numbers in run-time error backtraces]
 

Latest revision as of 17:14, 6 August 2022

English (en) suomi (fi)

A run-time error is an irreparable error condition that arises during the run-time, i.e. the execution of a program.

Behavior

The FPC inserts code to detect a vast number of error situations. If such a situation is encountered, the standard run-time library will initiate the termination of the program. A run-time error number, and the address the error occurred at is being printed. This is the safest and cheapest error treatment.

Comparative remarks

Compile-time errors

In contrast to compile-time errors, which the compiler detects during compilation, run-time errors depend on the state of the program, thus can not be foreseen in advance. If a compile-time error is encountered, no executable program is generated.

Exceptions

Run-time errors are the classical imperative approach in order to avoid inconsistent program states, which may eventually cause faulty program behavior. If FPC's sysUtils unit is included, all run-time errors become exceptions (cf. system.runTimeErrors for details). Unlike run-time errors those can be caught by try... except on...do ... end blocks, provided a mode allowing exceptions – such as {$mode ObjFPC} or {$mode Delphi} – is being used. A run-time error causes the program to terminate, while an exception may give the opportunity to “fix” the problem. This standard behavior of system.runError can be altered by assigning a non-nil value to system.errorProc.

See also