Download presentation
Presentation is loading. Please wait.
Published byGyles Harrington Modified over 9 years ago
1
Tracing, Logging, and Error Handling MacDonald Ch. 8 MIS 424 MIS 424 Professor Sandvig Professor Sandvig
2
Today Error Handling Objective Objective IIS handlers IIS handlers.NET handlers.NET handlers Try Catch blockTry Catch block Exception Class Exception Class Page-level trappingPage-level trapping Application-level trappingApplication-level trapping Global.asax Global.asax Web.config Web.config
3
Error Handling Goal: User NEVER sees yellow screen!! User NEVER sees yellow screen!! Handle errors gracefullyHandle errors gracefully Example: WWU, ThinkGeekExample: WWU, ThinkGeekWWUThinkGeekWWUThinkGeek Implementation: 1. Robust programming Validate all user inputs Validate all user inputs Handle foreseeable problems Handle foreseeable problems 2. Trap all errors Handle gracefully Handle gracefully Error Pages Error Pages Log errors Log errors
4
Error Handling A good programmer is someone who always looks both ways before crossing a one-way street. Doug Linder systems administrator
5
Error Handling - IIS Incorrect URLs may not reach.NET Examples: 404 error handled by IIS 404 error handled by IIS 404 error 404 error 404 error handled by.NET 404 error handled by.NET 404 error 404 error Request does not reach.NET IIS has dozens of error pages May edit pages & routing rules May edit pages & routing rules
6
IIS
7
.NET Request reaches.NET Extension:.aspx, asmx, etc. Extension:.aspx, asmx, etc. Two options: Handle Programmatically Handle Programmatically web.config redirect web.config redirect
8
ASP.NET Error Handling Tools Catch errors at three levels: 1. Line: Try...Catch…Finally blocks 2. Page: Page_error() 3. Application: Global.asax
9
Line-level Error Handling Try Catch Block Wrap high-risk code Database, web services Database, web services All code that interacts with external data All code that interacts with external data Benefits Provide handlers Provide handlers Graceful responseGraceful response Log errorLog error Continue code execution Continue code execution Easy to implement Easy to implement
10
Try Catch Block Syntax try{ // problematic code // problematic code} catch (Exception e) { // error handler code // error handler code}
11
Try Catch Can trap by type of exception Trap specific first, then general Trap specific first, then general Syntax: Catch objEx as FormatException { handler } Catch objEx as OleDbException { Handler } Catch objEx as Exception { Handler }
12
Exception Class Exception object thrown when exception occurs Contains info about problem Two flavors: General (exception base class) General (exception base class)exception base classexception base class Specific (sqlException class) Specific (sqlException class)sqlException classsqlException class
13
Exception Class Can also customize exceptions Provide specific information Provide specific information Select appropriate exception and add custom message List of exceptions List of exceptions List of exceptions List of exceptions Example: Dog class (from 324)
14
Exception Class Set dog weight:
15
Try Catch Block Where: Catch in.aspx page Catch in.aspx page Errors will bubble up Errors will bubble up Provide user with nice message Provide user with nice message Log error Log error
16
Exception Handling Exceptions types Listed in MSDN documentation Listed in MSDN documentation System.Data.SqlClient SqlException ClassSystem.Data.SqlClient SqlException ClassSqlException ClassSqlException Class Try Catch Example: source, output Example: source, outputsourceoutputsourceoutput WriteErrorLog.cs WriteErrorLog.cs WriteErrorLog.cs
17
Page Level error trapping Catches unhandled exceptions on Page void Page_Error() { //handle error (write to log, redirect, etc.) //handle error (write to log, redirect, etc.)} Example: source, output Example: source, outputsourceoutputsourceoutput
18
Page_Error Limitation Page execution is terminated Page execution is terminated Labels not displayed Labels not displayed User sees text error message User sees text error message Graceful response: Graceful response: Redirect user to an error pageRedirect user to an error page Better choice: Better choice: Handle at application levelHandle at application level
19
Application Level Catches all unhandled exceptions Including 404s Including 404s Examples: Amazon, Netflix AmazonNetflixAmazonNetflix Two options: Global.asax Global.asax Web.Config file Web.Config file
20
Application Level Global.asax Handles application events Application start Application start Application end Application end Session start Session start Session end Session end Application_Error() Application_Error() UnhandledError.aspx source, output sourceoutputsourceoutput
21
Application Level Web.config Last in line Last in line Redirects user to an error page Syntax: </customErrors>
22
web.config CustomErrors section Redirect to error page Can configure different pages for different error codes Set on individual folders Documentation: web.config web.config
23
Application Level Web.config Disadvantage: cannot get error details for log Disadvantage: cannot get error details for log Global.asax is better option Error details accessible Error details accessible
24
Summary Error Trapping Objectives Objectives Handle exceptions gracefullyHandle exceptions gracefully Record details in log fileRecord details in log file 3 Levels 3 Levels 1.Line - Try Catch Block 2.Page - Page_Error() 3.Application Global.asax Application_Error Global.asax Application_Error web.config web.config
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.