230 likes | 247 Views
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!!
E N D
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!! • Handle errors gracefully • Example: WWU, ThinkGeek, Reddit (refresh)
Error Handling • Implementation: • Robust programming • Validate all user inputs • Handle foreseeable problems • Trap all errors • Handle gracefully • Display message • Log errors
Error Handling A good programmer is someone who always looks both ways before crossing a one-way street. Doug Linder systems administrator
Error Handling • Exceptions should be exceptional • Avoid them • Expensive to handle • Resource intensive • Do not use for: • Data Validation • Manage buggy code
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
Error Handling - IIS • Incorrect URLs may not reach .NET • Examples: • 404 error handled by IIS • IIS has dozens of error pages • May edit messages
.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
Exception Class • Thrown when exception occurs • Contains details • Documentation: • Exception base class • Specific: sqlException class • Contains information specific to exception type
Exception Class • Can throw custom exceptions • Provide specific information • Example: Dog class (from 324)
.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
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
Try Catch Block Syntax try { // problematic code } catch (Exception e) { // error handler code }
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 }
Try Catch Block • Where: • Catch in controller • Errors will bubble up • Provide user with nice message • Log error • Try Catch • Example (see handout)
HandleError Attribute • Decorate ActionMethods • Redirect to error page • Exception sent to error page
Application Level • Global.asax • Application_Error() handler • Catches all unhandled exceptions • Including 404s • Log exception • Redirect to error page
Application Level • Global.asax • Handles application events • Application start • Application end • Session start • Session end • Application_Error() • Example (see handout)
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
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