ASP.Net/Development/On Error

Материал из .Net Framework эксперт
Версия от 11:51, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

On Error GoTo HandleError

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    On Error GoTo HandleError
    Dim TempResult as Integer
    TempResult = txtNumber1.Text / txtNumber2.Text
    lblResult.Text = "Result: " & TempResult
    Exit Sub
    
    HandleError:
        lblError.Text = "Unable to calculate due to this error: " _
            & Err.Description
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>On Error Go To</TITLE>
</HEAD>
<BODY  TEXT="black" LINK="darkred" VLINK="darkred" ALINK="red" LEFTMARGIN="40" TOPMARGIN="60">
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label
    id="lblMessage1"
    runat="Server"
    Text="Enter a Number"
/>
<BR>
<asp:TextBox
    id="txtNumber1"
    runat="Server"
/>
<BR><BR>
<asp:Label
    id="lblMessage2"
    runat="Server"
    Text="Enter another Number"
/>
<BR>
<asp:TextBox
    id="txtNumber2"
    runat="Server"
/>
<BR><BR>
<asp:Label
    id="lblResult"
    runat="Server"
    Text="Result: "
/>
<BR>
<asp:Label
    id="lblError"
    runat="Server"
/>
<BR>
<asp:Button 
    id="butOK"
    text="  Go  "
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>



On Error Resume

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    On Error GoTo HandleError
    Dim TempResult as Integer
    TempResult = txtNumber1.Text / txtNumber2.Text
    lblResult.Text = "Result: " & TempResult
    Exit Sub
    
    HandleError:
        lblError.Text = "Unable to calculate due to this error: " _
            & Err.Description
        Resume Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Go To with Resume</TITLE>
</HEAD>
<BODY TEXT="black" LINK="darkred" VLINK="darkred" ALINK="red" LEFTMARGIN="40" TOPMARGIN="60">
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label
    id="lblMessage1"
    runat="Server"
    Text="Enter a Number"
/>
<BR>
<asp:TextBox
    id="txtNumber1"
    runat="Server"
/>
<BR><BR>
<asp:Label
    id="lblMessage2"
    runat="Server"
    Text="Enter another Number"
/>
<BR>
<asp:TextBox
    id="txtNumber2"
    runat="Server"
/>
<BR><BR>
<asp:Label
    id="lblResult"
    runat="Server"
    Text="Result: "
/>
<BR>
<asp:Label
    id="lblError"
    runat="Server"
/>
<BR>
<asp:Button 
    id="butOK"
    text="  Go  "
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
</Font>
</Form>
</BODY>
</HTML>



Send out an email in case of page error

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Mail" %>
<html>
<head>
   <title>Throw an Error</title>
   <script runat="server">
      Sub Page_Load()
         Dim NullText As String = Nothing
         Message.Text = NullText.ToString()
      End Sub
      Sub Page_Error(Source As Object, E As EventArgs)
         Dim ex As Exception = Server.GetLastError()
         If Not ex Is Nothing Then
            Dim Mail as New MailMessage()
            "Change the values below to valid email addresses
            Mail.To = "yourEmailAddress@yourServer.ru"
            Mail.From = "fromEmail@fromEmail.ru"
            Mail.Subject = "error"
            Mail.Body = "An Exception occurred in page " & _
               Request.RawUrl & ":" & vbCrLf
            Mail.Body &= ex.ToString() & vbCrlf & vbCrlf
            Mail.Body &= "was handled from Page_Error."
            "If your SMTP server is not local, change the property below
            "   to a valid server or domain name for the SMTP server
            SmtpMail.SmtpServer = "localhost"
            SmtpMail.Send(Mail)
            Server.ClearError()
         End If
         Response.Write("An error has occurred. " & _
            "The site administrator has been notified.<br/>" & _
            "Please try your request again later.")
      End Sub
   </script>
</head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>