Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception Handling .NET MVC

Similar presentations


Presentation on theme: "Exception Handling .NET MVC"— Presentation transcript:

1 Exception Handling .NET MVC
MIS Professor Sandvig 11/10/2018 Exception Handling .NET MVC MIS 424 Professor Sandvig

2 MIS 324 -- Professor Sandvig
11/10/2018 Today Exception Handling Objective IIS handlers .NET Exception Class Techniques Try Catch block HandleError Attribute Application-level trapping Global.asax

3 MIS 324 -- Professor Sandvig
11/10/2018 Error Handling Goal: User NEVER sees yellow screen!! Handle errors gracefully Example: WWU, ThinkGeek, Reddit (refresh)

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

5 MIS 324 -- Professor Sandvig
11/10/2018 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 Expensive to handle
Avoid them Expensive to handle Resource intensive Do not use for: Data Validation Manage buggy code

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

8 MIS 324 -- Professor Sandvig
11/10/2018 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 MIS 324 -- Professor Sandvig
11/10/2018 IIS

10 MIS 324 -- Professor Sandvig
11/10/2018 .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 MIS 324 -- Professor Sandvig
11/10/2018 Exception Class Thrown when exception occurs Contains details Documentation: Exception base class Specific: sqlException class Contains information specific to exception type

12 MIS 324 -- Professor Sandvig
11/10/2018 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
MIS Professor Sandvig 11/10/2018 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 MIS 324 -- Professor Sandvig
11/10/2018 Try Catch Block Syntax try { // problematic code } catch (Exception e) // error handler code

16 MIS 324 -- Professor Sandvig
11/10/2018 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)

17 MIS 324 -- Professor Sandvig
11/10/2018 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 MIS 324 -- Professor Sandvig
11/10/2018 Application Level Global.asax Application_Error() handler Catches all unhandled exceptions Including 404s Log exception Redirect to error page

20 MIS 324 -- Professor Sandvig
11/10/2018 Application Level Global.asax Handles application events Application start Application end Session start Session end Application_Error() Example (see handout)

21 MIS 324 -- Professor Sandvig
11/10/2018 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 MIS 324 -- Professor Sandvig
11/10/2018 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.asax Application_Error


Download ppt "Exception Handling .NET MVC"

Similar presentations


Ads by Google