QP School

Full Version: Exception handling in Object Pascal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
program ExceptionHandlingExample;
var
  num1, num2, result: Integer;
begin
  try
    Write('Enter the first number: ');
    ReadLn(num1);

    Write('Enter the second number: ');
    ReadLn(num2);

    if num2 = 0 then
      raise Exception.Create('Division by zero is not allowed.');

    result := num1 div num2;
    WriteLn('Result: ', result);
  except
    on E: Exception do
      WriteLn('Exception caught: ', E.Message);
  end;
end.