|
xxx
Catch Errors in Your Code
by Charles Carroll
There are many tools to catch errors
in your page
-
web.config settings that let a
specific .aspx file catch every error on your site
-
page settings that let any given
page specify a specific .aspx page that catches any errors on that page
-
error handling code in that page
which is what we will discuss here
Error trapping in ASP.net is
structured to solve two programming problems other error trapping schemes (ON
ERROR RESUME NEXT notably) do not address:
-
If multiple errors occur on a page,
provide an easy mechanism to intercept all errors
-
If a page calls an assembly that has
NO or incomplete error handling the page should be able to catch the error the
other code causes
The basic idea is:
TRY
your code
CATCH
here is error handling code
FINALLY
here is cleanup code
in a way that you can protect
an entire page with a CATCH. You can also ensure any "cleanup" code is executed
in the FINALLY block.
Here is a code sample that shows a
modest error handler that should be the bare minimum most pages have!
filename=\\experiments\\errorhandling\\catcherrors.aspx
<%@ Import Namespace="System.Data.sqlclient" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
TRY
Dim strConn as string
strconn ="server=xxx;uid=xxx;pwd=xxx"
DIM strSQL as string
strSQL="select * from junk"
Dim Conn as New SQLConnection(strConn)
Dim Cmd as New SQLCommand(strSQL,Conn)
Conn.Open()
myDataGrid.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
myDataGrid.DataBind()
CATCH sqlerr as SQLException
status.text="<br>SQLError# =" & sqlerr.Number & ", SQL Msg=" & sqlerr.Message & "<br>"
CATCH exc As Exception
status.text=status.text & "<br>Exception #" & exc.ToString() & "<br>"
END TRY
End Sub
</script>
<html><head>
<title>Basic Error Trapping Demo</title>
</head>
<body bgcolor="#FFFFFF">
<font face="Verdana"><h3>Error Trapping Demo</h3></font><br>
Query Results<br>
<asp:literal id="status" runat="server"/>
<br>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="100%"
BackColor="white"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
Headerstyle-BackColor="lightblue"
Headerstyle-Font-Size="10pt"
Headerstyle-Font-Style="bold"
MaintainState="false"
/>
</body></html>
The beauty of this system is the
nesting support, i.e. if you want to handle the error f
TRY
...your code for task1...
TRY
... your code for task2..
CATCH
... your error handling for task2 ...
FINALLY
... your cleanup code for task2...
TRY
... your error code for task3...
CATCH
... your code for task3...
FINALLY
... your error code for task3...
TRY
... your error code for task4...
CATCH
... your code for task4...
FINALLY
... your error code for task4...
... task 5 code ...
CATCH
here is error code for task1,task5,task6 and any ASSEMBLY
unhandled exceptions
FINALLY
here is cleanup code for task1,task5,task6 and any ASSEMBLY
unhandled exceptions
As long as you make at least one
handler for all your code on a page you control the error message for any
unexpected errors.
 |  |  |
 |
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too.
a $5 tip buys him lunch at McDonalds,
a $20 tip buys his kid Hitoshi a new computer game,
a $39 tip buys his daughter Michiko a few nice outfits.
See our donor list.
|  |
 |  |  |
|
|
|
|