1 / 22

Exception Handling .NET MVC

Exception Handling .NET MVC. MIS 424 Professor Sandvig. Today. Exception Handling Objective IIS handlers .NET Exception Class Techniques Try Catch block HandleError Attribute Application-level trapping Global.asax. Error Handling. Goal: User NEVER sees yellow screen!!

pandrew
Download Presentation

Exception Handling .NET MVC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception Handling.NET MVC MIS 424 Professor Sandvig

  2. Today • Exception Handling • Objective • IIS handlers • .NET • Exception Class • Techniques • Try Catch block • HandleError Attribute • Application-level trapping • Global.asax

  3. Error Handling • Goal: • User NEVER sees yellow screen!! • Handle errors gracefully • Example: WWU, ThinkGeek, Reddit (refresh)

  4. Error Handling • Implementation: • Robust programming • Validate all user inputs • Handle foreseeable problems • Trap all errors • Handle gracefully • Display message • Log errors

  5. Error Handling A good programmer is someone who always looks both ways before crossing a one-way street. Doug Linder systems administrator

  6. Error Handling • Exceptions should be exceptional • Avoid them • Expensive to handle • Resource intensive • Do not use for: • Data Validation • Manage buggy code

  7. Error Handling • Where: • IIS • Errors that do not reach .NET • 404, 500, … • .NET • Controller • Try Catch – line of code • OnException – entire controller • Application • Global.aspx

  8. Error Handling - IIS • Incorrect URLs may not reach .NET • Examples: • 404 error handled by IIS • IIS has dozens of error pages • May edit messages

  9. IIS

  10. .NET • Request reaches .NET • At least six options for handling • All exceptions throw .NET exception object • Contains details about exception • See message in Yellow screen

  11. Exception Class • Thrown when exception occurs • Contains details • Documentation: • Exception base class • Specific: sqlException class • Contains information specific to exception type

  12. Exception Class • Can throw custom exceptions • Provide specific information • Example: Dog class (from 324)

  13. .NET Exception Handling • Six options (at least) for handling • Three easy options: • Try Catch • line of code • HandleError Attribute – • single action method • Application_Exception handler • Global.asax • Entire application

  14. Line-level Error Handling • Try Catch Block • Wrap high-risk code • Database, web services • Any code that interacts with external resources • Benefits • Provide handlers • Graceful response • Log error • Continue code execution • Easy to implement

  15. Try Catch Block Syntax try { // problematic code } catch (Exception e) { // error handler code }

  16. Try Catch • Can trap by type of exception • Trap specific first, general last • Syntax: Catch (FormatException ex) { handler } Catch (OleDbException ex) { Handler } Catch (Exception ex) { Handler }

  17. Try Catch Block • Where: • Catch in controller • Errors will bubble up • Provide user with nice message • Log error • Try Catch • Example (see handout)

  18. HandleError Attribute • Decorate ActionMethods • Redirect to error page • Exception sent to error page

  19. Application Level • Global.asax • Application_Error() handler • Catches all unhandled exceptions • Including 404s • Log exception • Redirect to error page

  20. Application Level • Global.asax • Handles application events • Application start • Application end • Session start • Session end • Application_Error() • Example (see handout)

  21. Other Options • OnException method • Handles all exceptions in: • Controller or • Application • Web.config • Redirect to error page • No logging • More information: • Exception handling in ASP.NET MVC

  22. Summary • Error Trapping • Objectives • Handle exceptions gracefully • Record details in log file • 3 Levels • Line - Try Catch Block • Controller action method • HandleError Attribute • Application • Global.asaxApplication_Error

More Related