2

I am building a ASP.NET Web Api service and I would like to create centralized exception handling code.

I want to handle different types of exceptions in different ways. I will log all exceptions using log4net. For some types of exceptions I will want to notify an administrator via email. For some types of exceptions I want to rethrow a friendlier exception that will be returned to the caller. For some types of exceptions I want to just continue processing from the controller.

But how do I do that? I am using an Exception Filter Attribute. I have this code working. The attribute is registered properly and the code is firing. I just want to know how I can continue if certain types of exceptions are thrown. Hope that makes sense.

public class MyExceptionHandlingAttribute : ExceptionFilterAttribute
{
  public override void OnException(HttpActionExecutedContext actionExecutedContext)
  {
    //Log all errors
    _log.Error(myException);

    if(myException is [one of the types I need to notify about])
    {
      ...send out notification email
    }

    if(myException is [one of the types that we continue processing])
    {
      ...don't do anything, return back to the caller and continue
      ...Not sure how to do this.  How do I basically not do anything here?
    }

    if(myException is [one of the types where we rethrow])
    {
      throw new HttpResponseException(new HttpResponseMessage(StatusCode.InternalServerError)
      {
        Content = new StringContent("Friendly message goes here."),
        ReasonPhrase = "Critical Exception"
      });
    }
  }
}
4

1 回答 1

2

For some types of exceptions I want to just continue processing from the controller. But how do I do that?

By writing try..catch where you want this behaviour to occur. See Resuming execution of code after exception is thrown and caught.

To clarify, I assume you have something like this:

void ProcessEntries(entries)
{
    foreach (var entry in entries)
    {
        ProcessEntry(entry);
    }
}

void ProcessEntry(entry)
{
    if (foo)
    {
        throw new EntryProcessingException();
    }
}

And when EntryProcessingException is thrown, you actually don't care and want to continue execution.


If this assumption is correct: you can't do that with a global exception filter, as once an exception is caught, there's no returning execution to where it was thrown. There is no On Error Resume Next in C#, especially not when the exceptions are handled using filters as @Marjan explained.

So, remove EntryProcessingException from your filter, and catch that specific exception by changing the loop body:

void ProcessEntries(entries)
{
    foreach (var entry in entries)
    {
        try
        {
            ProcessEntry(entry);
        }
        catch (EntryProcessingException ex)
        {
            // Log the exception
        }
    }
}

And your loop will happily spin to its end, but throw on all other exceptions where it will be handled by your filter.

于 2013-10-29T15:29:55.197 回答