No, sorry, I was wrong. *This* is the Code of the Day:
No, sorry, I was wrong. *This* is the Code of the Day:
try
...
except
On E:Exception do
begin
raise Exception.Create(E.Message);
end;
end;
try
...
except
On E:Exception do
begin
raise Exception.Create(E.Message);
end;
end;
Had to make sure that Ron Lawrence saw this.
ReplyDeleteDoh! ><
ReplyDeleteAlthough... I have some of those myself - albeit only with a raise;
ReplyDeleteLars -- please tell me you are making that up. ;-)
ReplyDeleteNope - Sometimes you need to take a look at certain conditions in the debugger when an exception occurs.
ReplyDeletelol....
ReplyDeleteI too have done that, so I could inspect the exception in the debugger before re-raising it. But as Lars said, a simple raise is enough for that purpose.
ReplyDeleteNick Hodges catching an exception and re-raising it can be helpful when you want to do something only when the exception occurs, like logging an error with CodeSite f.i.
ReplyDeleteAt least it's better than:
ReplyDeletetry
...
except
// nothing to see here
end;
I've seen this nugget in production code, comment included.
ReplyDeleteprocedure NoNotNot;
begin
if long_complex_condition then
else DoSomething; // Take good care of Elsie here
end;
Haha...nice!
ReplyDeleteKenneth Cochran I once wrote a little regex to go through and change all of those to call the MadExcept "silently log this" handler. It was slightly disturbing that it got 1800-odd hits. Then there's this sort of thing:
ReplyDeleteexcept
on E:Exception do
if E is ESomething then
...
else if E is EOther then
...
end;
Kenneth Cochran Kenneth, sometimes you want to just ignore the exception, so putting nothing between except and end will do that. I mean, for times when you don't give a rat's *ss what happens between try and except. Like if you're trying to delete a file that's not there, and if it fails, who cares.
ReplyDeleteI use a similar formwhenever I want to be able to place a breakpoint in the exception (as the debugger otherwise sometimes is unable to reconstruct the context well enough to evaluate variables etc.).
ReplyDeleteIt can also be useful in exception stack track logs to provide extra context info (same issue as above), or if you want to requalify the exception (though usually not with E.Message but with a custome exception wrapping the old one)
Nick Hodges Code in this example can be simplified as
ReplyDeletetry
...
except
raise;
end;
:)
I sometimes see using try..except raise; end; or try ... finally end; as alternative to begin..end block :)
Igor Schevchenko It is not only a shortening - a single raise will raise the same exception class as before, while the original code will always raise a simple Exception.
ReplyDeleteMost importantly, the stack from the original exception is still there - unlike where you catch and raise a new exception.
ReplyDeleteI found in project:
ReplyDeletetry
...
except
//
end;
Oleksandr -- Oh, that is all too common.
ReplyDeleteYo dawg, I herd you like exceptions...
ReplyDeleteThe empty try .. except block may be unavoidable - for example, when using Delphi's XML data bindings and trying to access an optional node.
ReplyDelete