Handle exceptions Class level or Class Library level without writing Try, Catch blocks. RRS feed

  • Question

  • I am working with ASP.NET MVC 4, .Net 4.5, Windows Azure. I handled all exceptions with out writing try catch blocks in Frontier(Web Application) using OnException() controller base class method. I am thinking to handle all exceptions in middle layers(BLL, DAL) in similar fashion without writing try catch blocks, because million lines of code already done without exception handling. Is this possible ? looking for suggestions.

    Ramesh Bolla

    Wednesday, July 10, 2013 7:13 AM


  • I don't see the value in doing that.  What are you going to do with the exception once you've caught it?  You don't want to silently ignore it as then your app will start acting funny and you won't know why.  I also tend to disagree that library (including middle layers) should catch exceptions for any reason other than rethrowing a different exception because you don't know how the library is being used.  This is especially true if you're unit testing code.  Provided you're catching exceptions in the controller then any exception in the lower layers will eventually be caught by the controller as they bubble up the stack frame.  As such you should already be getting the behavior you want.  The exception to the above would be code that is called outside a controller such as application-level code.  In that case ASP.NET provides facilities for responding to unhandled exceptions in non-request threads.  For other appdomains the CLR provides error mechanisms that can be used.

    Please post ASP.NET questions in the ASP.NET forums (http://forums.asp.net).

    Michael Taylor

    • Marked as answer by Min Zhu Thursday, July 18, 2013 8:23 AM
    Wednesday, July 10, 2013 2:08 PM