Error handling and debugging are crucial aspects of programming. Proper error handling ensures that your program can deal with unexpected situations gracefully, while debugging helps you identify and fix issues in your code. This chapter will cover techniques and tools for error handling and debugging in Pascal.
Runtime errors occur while the program is running. Common runtime errors include division by zero, file not found, and invalid input. Pascal provides mechanisms to handle such errors gracefully.
try...except
BlockThe try...except
block is used to handle exceptions (errors) that occur during program execution.
program TryExceptExample;
var
x, y, z: integer;
begin
x := 10;
y := 0;
try
z := x div y;
writeln('Result: ', z);
except
on E: EDivByZero do
writeln('Error: Division by zero');
end;
end.
In this example, the division by zero error is caught by the try...except
block, and a custom error message is displayed.
try...finally
BlockThe try...finally
block is used to ensure that certain code is executed regardless of whether an exception occurs or not. This is often used for resource cleanup.
program TryFinallyExample;
var
fileVar: text;
begin
assign(fileVar, 'output.txt');
rewrite(fileVar);
try
writeln(fileVar, 'Hello, World!');
finally
close(fileVar);
end;
end.
In this example, the file is closed regardless of whether an error occurs while writing to it.
Compile-time errors are detected by the compiler before the program runs. These errors include syntax errors, type mismatches, and undeclared variables. To fix compile-time errors, carefully read the compiler error messages and correct the issues in your code.
;
) or incorrect spelling of keywords.Debugging is the process of identifying and fixing errors in your code. Pascal provides several techniques and tools to help you debug your programs effectively.
The simplest way to debug your Pascal program is to use the writeln
statement to print variable values and track the flow of execution.
program DebuggingExample;
var
x, y, z: integer;
begin
x := 10;
y := 5;
writeln('x = ', x);
writeln('y = ', y);
z := x + y;
writeln('z = x + y = ', z);
z := x div y;
writeln('z = x div y = ', z);
end.
Many Pascal IDEs, such as Lazarus, come with built-in debuggers that allow you to set breakpoints, step through code, and inspect variable values.
Breakpoints are markers that you can set in your code to pause execution at specific points. This allows you to examine the state of your program and understand its behavior.
Stepping through code allows you to execute your program line by line. This helps you understand the flow of execution and identify the exact point where an error occurs.
While debugging, you can inspect the values of variables at different points in your program. This helps you verify that your variables are being updated correctly.
Pascal provides several predefined exception types that you can handle in your code. Some common exceptions include:
EConvertError
: Raised when a string-to-number conversion fails.EInOutError
: Raised during input/output operations (e.g., file not found).EOutOfMemory
: Raised when the system runs out of memory.program MultipleExceptionsExample;
var
x: integer;
str: string;
begin
str := 'abc';
try
x := StrToInt(str);
writeln('Converted value: ', x);
except
on E: EConvertError do
writeln('Error: Invalid number format');
on E: EOutOfMemory do
writeln('Error: Out of memory');
else
writeln('An unknown error occurred');
end;
end.
In this example, the EConvertError
is caught when trying to convert a non-numeric string to an integer.