250 likes | 539 Views
Tracing, Logging, and Error Handling MacDonald Ch. 8. MIS 424 Professor Sandvig. Today. Error Handling Objective Exception Class Trapping Try Catch block Page-level trapping Application-level trapping Global.asax Web.config. Error Handling. Goal: User NEVER sees yellow screen!!
E N D
Tracing, Logging, and Error HandlingMacDonald Ch. 8 MIS 424 Professor Sandvig
Today • Error Handling • Objective • Exception Class • Trapping • Try Catch block • Page-level trapping • Application-level trapping • Global.asax • Web.config
Error Handling • Goal: • User NEVER sees yellow screen!! • Handle errors gracefully • Example: WWU, ThinkGeek • Implementation: • Robust programming • Validate all user inputs • Handle foreseeable problems • Trap all errors • Handle gracefully • Error Pages • Log errors
Error Handling A good programmer is someone who always looks both ways before crossing a one-way street. Doug Linder systems administrator
Exception Class • Exception object thrown when exception occurs • Contains info about problem • Two flavors: • General (exception base class) • Specific (sqlException class)
Exception Class • Can also customize exceptions • Provide specific information • Select appropriate exception and add custom message • List of exceptions • Example: Dog class (from 324)
Exception Class • Set dog weight:
ASP.NET Error Handling Tools Catch errors at three levels: • Line: Try...Catch…Finally blocks • Page: Page_error() • Application: Global.asax
Line-level Error Handling • Try Catch Block • Wrap high-risk code • Database, web services • All code that interacts with external data • 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 Block • Where: • Catch in .aspx page • Errors will bubble up • Provide user with nice message • Log error
Exception Handling • Can trap by type of exception • Trap specific first, then general • Syntax: Catch objEx as FormatException { handler } Catch objEx as OleDbException { Handler } Catch objEx as Exception { Handler }
Exception Handling • Exceptions types • Listed in MSDN documentation • System.Data.SqlClientSqlException Class • Try Catch • Example: source, output • WriteErrorLog.cs
Page Level error trapping • Catches unhandled exceptions on Page void Page_Error() { //handle error (write to log, redirect, etc.) } • Example: source, output
Page_Error • Limitation • Page execution is terminated • Labels not displayed • User sees text error message • Graceful response: • Redirect user to an error page
Application Level • Catches all unhandled exceptions • Including 404s • Examples: Amazon, Netflix • Two options: • Global.asax • Web.Config file
Application Level • Global.asax • Handles application events • Application start • Application end • Session start • Session end • Application_Error() • UnhandledError.aspx source, output
Application Level • Web.config • Last in line • Redirects user to an error page • Syntax: <customErrorsdefaultRedirect="http://..." mode="On"> <error statusCode="404" redirect="filenotfound.htm" /> </customErrors>
Application Level • Web.config • Disadvantage: cannot get error details for log • Global.asax is better option • Error details accessible
Summary • Error Trapping • Objectives • Handle exceptions gracefully • Record details in log file • 3 Levels • Line - Try Catch Block • Page - Page_Error() • Application • Global.asaxApplication_Error • web.config