ASP.NET Tutorial/Development/HttpException
Содержание
Catch HttpException
<source lang="csharp">
<%@ Page Language="vb" EnableSessionState="false" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Session("foo") = "Foo" Catch HttpEx As HttpException Message.Text = "ERROR:</br>" Message.Text &= "Message: " & HttpEx.Message & "</br>" Message.Text &= "Help Link: " & HttpEx.HelpLink & "</br>" Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>" Message.Text &= "Source: " & HttpEx.Source & "</br>" Message.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>" Message.Text &= "Target Site: " & HttpEx.TargetSite.ToString() & "</br>" Message.Text &= "Http Error Message: " & HttpEx.GetHtmlErrorMessage() & "</br>" Message.Text &= "Http Error Code: " & HttpEx.GetHttpCode() & "</br>" Message.Text &= "String: " & HttpEx.ToString() & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="Message" forecolor="red" runat="server"/>
</body> </html></source>
Convert Exception to string
<source lang="csharp">
<%@ Page Language="vb" EnableSessionState="false" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Session("foo") = "Foo" Catch HttpEx As HttpException myMessage.Text = "ERROR:</br>" myMessage.Text &= "String Representation of Exception: " & _ HttpEx.ToString() & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="myMessage" forecolor="red" runat="server"/>
</body> </html></source>
Exception Message
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Throw New HttpException("Threw an error from Page_Load") Catch HttpEx As HttpException myMessage.Text = "ERROR:</br>" myMessage.Text &= "Message: " & HttpEx.Message & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="myMessage" forecolor="red" runat="server"/>
</body> </html></source>
Get base exception
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Dim myHttpEx As _ New HttpException("This is the original exception") Dim myHttpEx2 As _ New HttpException("This is a nested exception", myHttpEx) Throw New HttpException("Threw an exception from Page_Load", _ myHttpEx2) Catch HttpEx As HttpException Dim InnerHttpEx As HttpException InnerHttpEx = HttpEx.InnerException Message.Text = "ERROR:</br>" Message.Text &= "Message: " & HttpEx.Message & "</br>" Message.Text &= "Inner Exception Message: " & _ InnerHttpEx.Message & "</br>" Message.Text &= "Base Exception Message: " & _ InnerHttpEx.GetBaseException.Message & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="Message" forecolor="red" runat="server"/>
</body> </html></source>
Get error message
<source lang="csharp">
<%@ Page Language="vb" EnableSessionState="false" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Server.Execute("Foo.aspx") Catch HttpEx As HttpException Message.Text = "ERROR:</br>" Message.Text &= "Http Error Message: " & _ HttpEx.GetHtmlErrorMessage() & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="Message" forecolor="red" runat="server"/>
</body> </html></source>
GetHttpCode
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Server.Execute("Foo.aspx") Catch HttpEx As HttpException Message.Text = "ERROR:</br>" Message.Text &= "Http Status Code: " & _ HttpEx.GetHttpCode() & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="Message" forecolor="red" runat="server"/>
</body> </html></source>
HelpLink
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Dim myHttpEx As _ New HttpException("Threw an exception from Page_Load") myHttpEx.HelpLink = "file://C:/Data.htm" Throw myHttpEx Catch HttpEx As HttpException myMessage.Text = "ERROR:</br>" myMessage.Text &= "Message: " & HttpEx.Message & "</br>" myMessage.Text &= "Help Link: " & HttpEx.HelpLink & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="myMessage" forecolor="red" runat="server"/>
</body> </html></source>
HttpException("Threw an error from Page_Load", 100)
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Throw New HttpException("Threw an error from Page_Load", 100) Catch HttpEx As HttpException Message.Text = "ERROR:</br>" Message.Text &= "Message: " & HttpEx.Message & "</br>" Message.Text &= "Error Code: " & HttpEx.ErrorCode & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="Message" forecolor="red" runat="server"/>
</body> </html></source>
InnerException
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Dim myHttpEx As _ New HttpException("This is a nested exception") Throw New HttpException("Threw an exception from Page_Load", _ myHttpEx) Catch HttpEx As HttpException Dim InnerHttpEx As HttpException InnerHttpEx = HttpEx.InnerException myMessage.Text = "ERROR:</br>" myMessage.Text &= "Message: " & HttpEx.Message & "</br>" myMessage.Text &= "Inner Exception Message: " & _ InnerHttpEx.Message & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="myMessage" forecolor="red" runat="server"/>
</body> </html></source>
Source of exception
<source lang="csharp">
<%@ Page Language="vb" EnableSessionState="false" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Session("foo") = "Foo" Catch HttpEx As HttpException myMessage.Text = "ERROR:</br>" myMessage.Text &= "myMessage: " & HttpEx.Message & "</br>" myMessage.Text &= "Source: " & HttpEx.Source & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="myMessage" forecolor="red" runat="server"/>
</body> </html></source>
Stack trace
<source lang="csharp">
<%@ Page Language="vb" %> <html> <head>
<script runat="server"> Sub Page_Load() Try ThrowMeAnException Catch HttpEx As HttpException myMessage.Text = "ERROR:</br>" myMessage.Text &= "Message: " & HttpEx.Message & "</br>" myMessage.Text &= "Stack Trace: " & HttpEx.StackTrace & "</br>" End Try End Sub Sub ThrowMeAnException() Throw New HttpException("Threw an error from ThrowMeAnException") End Sub </script>
</head> <body>
<asp:label id="myMessage" forecolor="red" runat="server"/>
</body> </html></source>
Target site
<source lang="csharp">
<%@ Page Language="vb" EnableSessionState="false" %> <html> <head>
<script runat="server"> Sub Page_Load() Try Session("foo") = "Foo" Catch HttpEx As HttpException myMessage.Text = "ERROR:</br>" myMessage.Text &= "Message: " & HttpEx.Message & "</br>" myMessage.Text &= "Target Site: " & HttpEx.TargetSite.Name & "</br>" End Try End Sub </script>
</head> <body>
<asp:label id="myMessage" forecolor="red" runat="server"/>
</body> </html></source>