2

Since ELMAH logs UNHANDLED exceptions and I want to log all errors via ELMAH, does that mean I should not be using try-catch block anymore anywhere on my code? If I do not use it, it will show the yellow screen of death, which I believe I can be handled through the custom error pages.

So, I think try-catch block will be useless if you use ELMAH, am I right? If not, what are the use cases of using try catch with ELMAH?

Chris
  • 87
  • 2
  • 8
  • 1
    I think you've got it wrong! You always need to handle exceptions, ELMAH is useful for letting you know about unhandled exceptions, about places have potentially bugs. You can fix those places by extra checking(if&else for example) and then you may continue executing code or show the user a meaningful message or let the exception bubble to be handled until handled by ELMAH. – A.Akram Jun 03 '15 at 21:14
  • 1
    Usually a combination. Catch the errors you can do something about, let ELMAH catch the rest. http://stackoverflow.com/questions/21862753/log-exceptions-handled-in-try-catch-with-elmah – Steve Greene Jun 03 '15 at 21:16
  • @SteveGreene what if I want to log to ELMAH the errors I caught from the catch block as well? I want to log all errors to ELMAH, including places in my code where I expect to fail (like DB access) – Chris Jun 03 '15 at 21:53
  • Use ErrorSignal to log caught errors. http://blog.elmah.io/how-to-log-errors-to-elmah-programmatically/ – Steve Greene Jun 03 '15 at 23:57
  • Thanks @SteveGreene. Is there a chance here that I can accept your answer? – Chris Jun 05 '15 at 15:32

2 Answers2

4

With ELMAH you can catch errors, handle them and still log the issue using ErrorSignal. http://blog.elmah.io/how-to-log-errors-to-elmah-programmatically/

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
1

You are wrong. Only because ELMAH provides some logging features (and more) around unhandled exceptions doesn't mean that all exceptions are suggested to be unhandled.

You should keep using exceptions the way you expect them to be handled such as providing proper handling such as further diagnostics or even business logic around them.

Example: if your application integrates with a third party service which is down you'll likely end up having an exception which you should handle, e. g.:

try {
    .. call third party service
} catch (TimeOutException toe) {
    .. handle such as redirection
}
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
  • Thanks! For your example, what if I want to log that time out exception in ELMAH, how should I do it? I can re-throw but the "handle such as redirection" thing will not be hit if I place the re-throw before. If I place it after the redirection, I think the re-throw will not be hit as well. – Chris Jun 03 '15 at 21:45
  • You cannot redirect *and* throw; you do either one or the other. Do logging first [logging with ELMAH](http://www.asp.net/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/logging-error-details-with-elmah-cs), the redirect. Done. If you throw an exception and don't handle it yourself your application will blow up. – Quality Catalyst Jun 03 '15 at 22:17