Exception - Exemples

Exception = class (TObject)Interface de ExceptionExemples de Exception
try
   raise Exception.Create('Hello exception');
except
   on E : Exception do begin
      PrintLn(E.ClassName);
      PrintLn(E.Message);
   end;
end;

type EMyException = class (Exception);

try
   raise EMyException.Create('Hello again');
except
   on E : Exception do begin
      PrintLn(E.ClassName);
      PrintLn(E.Message);
   end;
end;

procedure DoSomeException(useMyException : Boolean);
begin
   if useMyException then
      raise EMyException.Create('boom')
   else raise Exception.Create('boom')
end;

try
   PrintLn('Raise an exception');
   DoSomeException(False);
except
   on E : EMyException do
      PrintLn('There are many like it, but this one is mine! ' + E.StackTrace);
   on E : Exception do
      PrintLn('Got another type of exception at ' + E.StackTrace);
end;

try
   PrintLn('Raise an exception');
   DoSomeException(True);
except
   on E : EMyException do
      PrintLn('There are many like it, but this one is mine! ' + E.StackTrace);
   on E : Exception do
      PrintLn('Got another type of exception at ' + E.StackTrace);
end;