ArjaaAine
ArjaaAine

Reputation: 896

What if the catch statement code causes an error?

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?

Upvotes: 0

Views: 157

Answers (1)

Piyush
Piyush

Reputation: 33

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.

Upvotes: 1

Related Questions