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;

Comments

  1. Had to make sure that Ron Lawrence  saw this.

    ReplyDelete
  2. Although... I have some of those myself - albeit only with a raise;

    ReplyDelete
  3. Lars -- please tell me you are making that up.  ;-)

    ReplyDelete
  4. Nope - Sometimes you need to take a look at certain conditions in the debugger when an exception occurs.

    ReplyDelete
  5. I 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.

    ReplyDelete
  6. Nick 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.

    ReplyDelete
  7. At least it's better than:
    try
    ...
    except
    // nothing to see here
    end;

    ReplyDelete
  8. I've seen this nugget in production code, comment included.

    procedure NoNotNot;
    begin
      if long_complex_condition then
       else DoSomething;  // Take good care of Elsie here
    end;

    ReplyDelete
  9. Kenneth 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:

    except
    on E:Exception do
      if E is ESomething then
        ...
      else if E is EOther then
        ...
    end;

    ReplyDelete
  10. 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.

    ReplyDelete
  11. I 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.).

    It 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)

    ReplyDelete
  12. Nick Hodges Code in this example can be simplified as
    try
      ...
    except
      raise;
    end;

    :)

    I sometimes see using try..except raise; end; or try ... finally end; as alternative to begin..end block :)

    ReplyDelete
  13. 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.

    ReplyDelete
  14. Most importantly, the stack from the original exception is still there - unlike where you catch and raise a new exception.

    ReplyDelete
  15. I found in project: 
    try
      ...
    except
    //
    end;

    ReplyDelete
  16. Oleksandr -- Oh, that is all too common.

    ReplyDelete
  17. Yo dawg, I herd you like exceptions...

    ReplyDelete
  18. The empty try .. except block may be unavoidable - for example, when using Delphi's XML data bindings and trying to access an optional node.

    ReplyDelete

Post a Comment