"> ">
Download presentation
Presentation is loading. Please wait.
Published byBenedict Barker Modified over 8 years ago
1
Application Tracing and Error Handling
2
Catching Exceptions with TRY … CATCH Sub Page_Load Dim conNorthwind As SqlConnection Dim cmdSelect As SqlCommand conNorthwind = New _ SqlConnection( "Server=localhost;UID=sa;pwd=secret;Database=Northwind;Connection _ TimeOut=15" ) cmdSelect = New SqlCommand( "select * from Products", conNorthwind ) Try conNorthwind.Open dgrdProducts.DataSource = cmdSelect.ExecuteReader() dgrdProducts.DataBind() conNorthwind.Close Catch Response.Write( "We're sorry, we are experiencing technical problems..." ) End Try End Sub
3
Catching Exceptions with TRY … CATCH TryCatch.aspx <asp:DataGrid ID="dgrdProducts" Runat="Server" />
4
Catching Exceptions with TRY … CATCH Sub Page_Load Dim conNorthwind As SqlConnection Dim cmdSelect As SqlCommand conNorthwind = New SqlConnection( "Server=localhost;UID=sa; _ pwd=secret;Database=Northwind;Connection Timeout=15" ) cmdSelect = New SqlCommand( "select * from Products", conNorthwind ) Try conNorthwind.Open dgrdProducts.DataSource = cmdSelect.ExecuteReader dgrdProducts.DataBind() conNorthwind.Close Catch objException As Exception Response.Write( "We're sorry, we are experiencing technical problems..." ) Response.Write( " " ) Response.Write( " Message: " & objException.Message ) Response.Write( " Source: " & objException.Source ) Response.Write( " Stack Trace: " & objException.StackTrace ) Response.Write( " Target Site: " & objException.TargetSite.Name ) End Try End Sub
5
Catching Exceptions with TRY … CATCH CatchException.aspx <asp:DataGrid ID="dgrdProducts" Runat="Server" /> Message: Returns a string that represents the error message Source: Returns a string representing the object or application that caused the error StackTrace: Returns a string that represents the methods called immediately before the error occurred TargetSite: Returns a MethodBase object that rerepresents the method that caused the error
6
Catch Particular Exceptions SqlException Sub Page_Load Dim conNorthwind As SqlConnection Dim cmdSelect As SqlCommand conNorthwind = New SqlConnection( "Server=localhost;UID=sa;pwd=secret; _ Database=Northwind;Connection TimeOut=15" ) cmdSelect = New SqlCommand( "select * from BadTable", conNorthwind ) Try conNorthwind.Open() dgrdProducts.DataSource = cmdSelect.ExecuteReader dgrdProducts.DataBind() conNorthwind.Close Catch objException As SqlException Dim objError As SqlError For each objError in objException.Errors Response.Write( " " & objError.Message ) Next End Try End Sub
7
Catch Particular Exceptions SqlException.aspx <asp:DataGrid ID="dgrdProducts" Runat="Server" /> SqlException
8
Catching Unhandled Exceptions in a Page Sub Page_Error Response.Write( "Sorry, there was an error:" ) Response.Write( " " ) Response.Write( Server.GetLastError.Message ) Server.ClearError() End Sub Sub Page_Load Dim txtTextBox As TextBox txtTextBox.Text = "Hello World!" End Sub PageError.aspx This page contains an error!
9
Tracing and Monitoring Your Application Sub Page_Load Trace.Warn( "Page Loading" ) End Sub Sub Button_Click( s As Object, e As EventArgs ) Trace.Warn( "The value of favColor is " & txtFavColor.Text ) lblMessage.Text = "Button Clicked!" End Sub Trace.aspx Enter your Favorite Color Favorite Color: <asp:TextBox ID="txtFavColor" Runat="Server" /> <asp:Button Text="Click Here!" OnClick="Button_Click" Runat="Server" /> <asp:Label ID="lblMessage" Runat="Server" />
10
Using the Debugger Web.Config
11
Using the Debugger Sub Page_Load Dim intcounter As Integer For intCounter = 0 to 10 lblCount.Text = intCounter.ToString() Next End Sub DebugThis.aspx <asp:Label id="lblCount" Runat="Server" /> Create a break point
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.