0

I am using Elmah for error reporting in my applications.

Usually if there's an error I catch it, craft a custom message and throw it back again.

catch (Exception ex)
{
    var e = new Exception("Failed to get Intake Statuses <br />" 
+ " (@PageNumber = " + pageNumber + ", @PageSize = " + pageSize + ".<br />" 
+ " Error: " + ex);

    ErrorLogger.LogErrorManually(e);

    throw new Exception(e.Message);
}

Now the issue arises if there is an error in the custom error that I am created.

What are the best practices to handle that? Do I create another sub Try/Catch?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
ArjaaAine
  • 886
  • 5
  • 14
  • 27
  • Is `LogErrorManually` prone to throwing? Perhaps it should try/catch itself. Otherwise, it doesn't look like you need to worry about it. If you did, another try/catch sounds like your best bet. – BradleyDotNET Oct 20 '14 at 20:13
  • Does the method calling this have a `try...catch` of its own? – Mephy Oct 20 '14 at 20:13
  • 10
    Throwing a new Exception will cause you to lose the stack trace. Consider instead just `throw;`, or throwing a new exception that includes the original exception as the inner exception. – Eric J. Oct 20 '14 at 20:14
  • What do you mean an error in the custom error? – John Saunders Oct 20 '14 at 20:15
  • 1
    `Do I created another nested Try/Catch loop?` Try/catch is not a loop. It's a statement. See [try/catch C3 Reference](http://msdn.microsoft.com/en-us/library/0yd65esw.aspx). – mason Oct 20 '14 at 20:17
  • 1
    Instead doing `throw new Exception(e.Message);` it would be much better to either do `throw new YourOwnCustomException("Your Message", ex);` to create your own custom exception with a message and having the original exception set as the inner exception or just do `throw;` to re-throw the original exception with all of it's information in place. You really should never throw the base `Exception` class, it is considered a bad practice. – Scott Chamberlain Oct 20 '14 at 21:41
  • @Eric J., Scott.. Thanks I will refactor my statements to do that. I wasn't aware of the ramification> – ArjaaAine Oct 20 '14 at 21:51
  • @mason. Yes you are correct. I miswrote. Bradley. The problem happened when the variable pageNumber was undefined, the main statement threw an error.. then the catch statement failed too. – ArjaaAine Oct 20 '14 at 21:52
  • 1
    Then you should check for null if you want to avoid nested try/catch. – mason Oct 20 '14 at 21:56
  • I could, but that would be a lot of work as every single method has a different custom error and it would mean writing 100 checks for null. I don't mind using nested try catch, i just wanted to know if it is the best way to approach the problem. – ArjaaAine Oct 20 '14 at 21:58

1 Answers1

1

You can do the following:

  1. Create a method say A with try catch and lets call your function whose catch you have given in description as B.
  2. In your B catch just use throw so that your stack trace will not go away.
  3. On exception in B catch it will navigate to catch A and thus you can show the message as you like it.
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Piyush
  • 33
  • 10
  • It would be a lot better if this was explained via a code example. – Scott Chamberlain Oct 20 '14 at 21:55
  • 1
    It took me a little reading to understand, but this makes sense. A code example though would be perfect for anyone else who stumbles upon this question. I will mark it as an answer afterwards. – ArjaaAine Oct 20 '14 at 21:57