Craig
Craig

Reputation: 36816

Get .NET MVC to completely ignore error

I have a legacy library in my ASP.NET MVC app that raises a lot of exceptions I need to ignore. I ignore these exceptions in Application_Error like this

protected void Application_Error()
{  
    if (exception is PolicyViolationException)
    {              
        Response.Clear();
        Server.ClearError();
    }
}

I know this is a code smell, but I can't do much about it at the moment.

Is there a way to stop them even getting to Application_Error?

Upvotes: 0

Views: 322

Answers (1)

Jeff S
Jeff S

Reputation: 7484

Use a Wrapper class (the Adapter Pattern). Then, instead of referencing the legacy library, you use the wrapper class. And the wrapper can handle (or ignore) the exceptions as needed.

class Legacy
{  public void DoThis() 
   { ... }
   public void DoThat()
   { ... }
}

class Wrapper 
{   Legacy _legacy;
    public Wrapper() { _legacy = new Legacy(); }
    public void DoThis()
    {
       try {
           _legacy.DoThis();
       }
       catch (PolicyViolationException exception) {
           //ignore
       }
    }
 ...
}

In this example, I would never reference the class Legacy. Instead, I would reference the class Wrapper. Then I don't have to worry about the exceptions because they won't get out of the Wrapper instance if I don't want them to.

Upvotes: 3

Related Questions