All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.delphi.rules.community-delphi.RaisingRawException.html Maven / Gradle / Ivy

There is a newer version: 1.12.1
Show newest version

Why is this an issue?

The purpose of exceptions is twofold - to provide clear reporting about what exceptional case the program has encountered, and to give the program enough information to recover if desired. If it's not known which error occurred, then it is difficult to know how to recover.

This can lead to overly general error handling scenarios, like blindly displaying a "something went wrong" message.

Raising a generic exception may also lead to overly general try/catch statements, as there is no other way to handle such an exceptional case. Code that catches specific exceptions is clearer and more resilient.

How to fix it

Use a specialized exception type (or define your own):

procedure EatIceCream(IceCream: TIceCream);
begin
  if IceCream.Melted then begin
    raise Exception.Create('The ice cream is melted');
  end
  else if IceCream.Flavour = 'mint' then begin
    raise Exception.Create('I hate mint');
  end;

  // ...
end;
procedure EatIceCream(IceCream: TIceCream);
begin
  if IceCream.Melted then begin
    raise EMeltedIceCream.Create('The ice cream is melted');
  end
  else if IceCream.Flavour = 'mint' then begin
    raise EFlavourError.Create('I hate mint');
  end;

  // ...
end;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy