ASP.Net/Server/Server Error

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

Examining the Last Error (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" %> <html> <head>

  <title>Examining the Last Error</title>
  <script runat="server">
     Sub CauseError(sender As Object, e As EventArgs)
        Dim x, y, z As Integer
        
        y = 1
        z = 0
        x = y / z
     End Sub
     Sub Page_Error(Source As Object, E As EventArgs)
        Dim LastError As Exception
        Dim ErrMessage As String
        LastError = Server.GetLastError()
        If Not LastError Is Nothing Then
           ErrMessage = LastError.Message
        Else
           ErrMessage = "No Errors"
        End If
        Response.Write("Last Error = " & ErrMessage & "

") Server.ClearError() End Sub </script>

</head> <body>

  <form runat="server">

Cause an Error to Occur...

     <asp:button text="CauseError" OnClick="CauseError" runat="server"/>
  </form>

</body> </html>

      </source>
   
  


Get last error and clear error stack trace (C#)

   <source lang="csharp">

<script Language="c#" runat="server">

 void PageLevelErrorTest()
 {
   // Remove opening try
   int[] array = new int[9];
   for(int intCounter=0; intCounter <= 9;intCounter++)
   {
      array[intCounter] = intCounter;
      Response.Write("The value of the counter is:" + intCounter + "
"); } // Remove catch and finally blocks } void Page_Error(object sender, EventArgs e) { Response.Write("Error occurred: " + Server.GetLastError().ToString()); Server.ClearError(); } void Page_Load() { PageLevelErrorTest(); }

</script> <%

 Response.Write("Function call completed" + "
");

%>

      </source>
   
  


Get string format server string (VB.net)

   <source lang="csharp">

<%@ Page Language="vb" %> <html>

  <head>
     <title>Error event example</title>
     <script runat="server">
        Sub Page_Load()
           Dim x, y, overflow As Integer
           x = 1
           y = 0
           overflow = x / y
        End Sub
        Sub Page_Error()
           Response.Write(Server.GetLastError.ToString())
           Server.ClearError
        End Sub
     </script>
  </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>
   
  


Server.ClearError (VB.net)

   <source lang="csharp">

<%@ Page Language="vb" %> <html>

  <head>
     <title>Error event example</title>
     <script runat="server">
        Sub Page_Load()
           Dim x, y, overflow As Integer
           x = 1
           y = 0
           overflow = x / y
        End Sub
        Sub Page_Error()
           Response.Write(Server.GetLastError.ToString())
           Server.ClearError
        End Sub
     </script>
  </head>

<body>

  <asp:label id="Message" runat="server"/>

</body> </html>

      </source>