ASP.NET Tutorial/Development/HttpException

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Catch HttpException

<%@ 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>


Convert Exception to string

<%@ 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>


Exception Message

<%@ 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>


Get base exception

<%@ 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>


Get error message

<%@ 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>


GetHttpCode

<%@ 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>


HelpLink

<%@ 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>


HttpException("Threw an error from Page_Load", 100)

<%@ 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>


InnerException

<%@ 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 of exception

<%@ 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>


Stack trace

<%@ 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>


Target site

<%@ 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>