This is rather theoretical question.
I've encountered this practice in a recent project - almost every procedure and function is wrapped into try/except clause like so:
function TMyClass.DoFoo(aBar: Integer): Boolean;
begin
try
.. do something .. // Proper checks for common errors inside
Result := True;
except
LogException(ClassName + '.DoFoo'); // Write exception to log
Result := False; // Indicate function did not performed what it was asked for
// Exit without re-raising exception
end;
end;
procedure TMyClass.Buzz(Sender: TObject);
begin
try
.. do something .. // Proper checks for common errors inside
except
LogException(ClassName + '.DoFoo'); // Write exception to log
// Exit without re-raising exception
end;
end;
This looks like a lot of repetition. However I can see some kind of logic behind it. If you wrap every procedure and function into try..except you would know approximate location of the exception and let the program continue working without popping crash messages at the user, but write them to a log for future analysis.
However this practice is considered to be evil. Why exactly is that?
EDIT:
- This is a question about project I'm going to work with, I did not invented it myself
- I'm well aware about madExcept and I use it in other projects
- Functions and procedures in that project are layed out as usual, there are no signs of procedures being written as functions for sole purpose of replacing exceptions flow
try ... catchrather than usingif-statements. Also, wrapping entire function bodies intry ... exceptlike that makes it seem like the developer hasn't spent any time thinking about what could go wrong, and instead just said "let's catch every erroneous situation and handle all of them the same way". - Michael