EAssertionFailed - Exemples

EAssertionFailed = class (Exception)Interface de EAssertionFailedExemples de EAssertionFailed
// Test procedure, with contract and assert
procedure Test(param : String);
require
   param<>'': 'Param must not be empty';
begin
   Assert(param<>'test', 'Param must not be "test"');

   PrintLn(param);
end;

// Main program
try
   var saisie:=InputBox('Test', 'Saisissez un texte', '');
   if saisie='toto' then
      raise Exception.Create('Too many tests with "toto" !');
   Test(saisie);
except
   on e: EAssertionFailed do begin
      PrintLn('Erreur récupérable');
   end;
   on e: Exception do begin
      PrintLn('Erreur irrécupérable');
      raise;
   end;
end;
PrintLn('OK');

// Quand on saisit "toto", on a une runtime error
// Quand on saisit "" ou "test", on a une erreur qui est trapée et le script passe à la suite