Test your Delphi knowledge: Exception handling

With this code, what will happen:

program FunWithDelphi;

{$APPTYPE CONSOLE}

uses
  Controls, SysUtils;

begin
  try
    raise TMouse.Create;
  except
    on E:Exception do
    begin
      Writeln(E.Classname, ': E is Exception');
    end;
    on E:TObject do
    begin
      Writeln(E.Classname, ': E is TObject');
    end;
    on E:TMouse do
    begin
      Writeln(E.Classname, ': E is TMouse');
    end;
  end;
  ReadLn;
end.
  • the program will print “TMouse: E is Exception”
  • the program will print “TMouse: E is TObject”
  • the program will print “TMouse: E is TMouse”
  • compilation fails
  • the program will terminate with a runtime error

Correct answer: the program will print “TMouse: E is TObject”
(Tested with Delphi 2009)

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s