Error Handling

This demonstration shows how you can include a block of code that will run only if an exception has been generated. We put the code that is likely to cause an error between try and except, and the code that is to run in the event of this error between except and end;. Our code enables us to see the message on the console window and to close the program without needing to restart Delphi 7. In other programs we may be able to return to a menu to allow users to continue with the parts of the program that work.

program InsertionSort3;
  {$APPTYPE CONSOLE}
  {$R+}
uses
  SysUtils;
var
  PosNums : array[1 .. 16] of integer =  (503, 087, 512, 061, 908,
                  170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703);

  NegNums : array[1 .. 16] of integer =  (-503, -087, -512, -061, -908,
                  -170, -897, -275, -653, -426, -154, -509, -612, -677, -765, -703);

procedure Display(iArray : array of integer);
var
  Count : integer;
begin
  for Count := 0 to high(iArray) do
    write(iArray[Count] : 5);
  writeln;
end;

procedure Sort(var intArray : array of integer);
var
  Inserted : Boolean;
  Current, ValueBelow, Insert, Temp : integer;
begin
  if high(intArray) < 1 then
    begin
      writeln('No sorting possible');
    end
  else
    begin
      for Insert := 1 to high(intArray) do
        begin
          Inserted := False;
          Current := Insert;
          Temp := intArray[Insert];
          while not inserted do
            begin
              try
                ValueBelow := intArray[Current - 1];
              except
                writeln('Failed during while loop with Current = ', Current);
                writeln('Please use only arrays in which the lowest value');
                writeln('already has the lowest index or, much better,');
                writeln('fix the bug!');
                readln;
                halt;
              end;
              if Temp > ValueBelow then
                begin
                  Inserted := True; //Position found;
                  intArray[Current] := Temp;
                end
              else
                begin
                   //Copy integer to the next position in the array
                   intArray[Current] := intArray[Current - 1];
                   dec(Current);
                 end;

            end;//while
        end;//for
     end;//if
end;//proc

begin
  writeln('Initial array of positive numbers:');
  Display(PosNums);
  Sort(PosNums);
  writeln('Sorted array of positive numbers:');
  Display(PosNums);
  writeln('Initial array of negative numbers:');
  Display(NegNums);
  Sort(NegNums);
  writeln('Sorted array of negative numbers:');
  Display(NegNums);
  readln;
end.

On running the program within the Delphi IDE (but not when we run InsertionSort3.exe directly) we see the following message ...

Breakpoint

Range error

then click on OK and press F9 to see our message (which appears also when we run the executable directly) ...

Our exception message

Our exception message

before pressing RETURN to quit gracefully. Again, the bug fix is the same as for program InsertionSort1 at the end of the Displaying Values section of this tutorial.

File operations are notorious for generating errors and deserve consideration for error handling. You can use if FileExists code as an easier alternative if it is the presence of the target file that is problematic.

Recent versions of Delphi invite you to place the bulk of your code between try and except. We aim to simplify our programs to make it easier for you to see what we are demonstrating, so we do not usually include error-handling code.

Programming - a skill for life!

Stepping through code, setting breakpoints, error handling and range checking