zoukankan      html  css  js  c++  java
  • 异常处理點滴

    异常处理重要

    l          捕获异常,写Log方便开发人和维护人追踪错误。

    l          抛出友好,明确,用户能明白的信息。

    方法一

    1 自定义异常

    Public Class MyException

        Inherits ArgumentException

        Public Sub New(ByVal message As [String])

            MyBase.New(message)

        End Sub

        Public Sub New(ByVal message As [String], ByVal inner As Exception)

            MyBase.New(message, inner)

        End Sub

    End Class

    2 启用自定义错误处理

    <customErrors defaultRedirect="HttpErrorPage.aspx" mode="Off">

    3 显示异常

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            ex = DirectCast(Server.GetLastError(), HttpException)

            Dim safeMsg As String = [String].Empty

            ' Filter for Error Codes and set text

            If ex.ErrorCode >= 400 AndAlso ex.ErrorCode < 500 Then

                ex = New HttpException(ex.ErrorCode, "Your file could not be found or " & "there was an access problem.", ex)

            ElseIf ex.ErrorCode > 499 Then

                ex = New HttpException(ex.ErrorCode, "There was an error on the server.", ex)

            Else

                ex = New HttpException(ex.ErrorCode, "There was a problem " & "with the web site.", ex)

            End If

            ' Log the exception and notify system operators

            ExceptionUtility.LogException(ex, "HttpErrorPage")

            ExceptionUtility.NotifySystemOps(ex)

            ' Fill the page fields

            exMessage.Text = ex.Message

            exTrace.Text = ex.StackTrace

            ' Show Inner Exception fields for local access

            If Not ex.InnerException Is Nothing Then

                innerTrace.Text = ex.InnerException.StackTrace

                innerMessage.Text = ex.InnerException.Message

            End If

            ' Clear the error from the server

            Server.ClearError()

        End Sub

    4 异常写入Log

    Imports System.IO

    Imports System.Web

    ' Create our own utility for exceptions

    Public NotInheritable Class ExceptionUtility

        ' All methods are static, so this can be private

        Private Sub New()

        End Sub

        ' Log an Exception

        Public Shared Sub LogException(ByVal exc As Exception, ByVal source As String)

            ' Include enterprise logic for logging exceptions

            ' Get the absolute path to the log file

            Try

                Dim logFile As String = "App_Data/Error/" + DateTime.Today.ToString("yy-MM-dd") & ".txt"

                If Not File.Exists(System.Web.HttpContext.Current.Server.MapPath(logFile)) Then

                    File.Create(System.Web.HttpContext.Current.Server.MapPath(logFile)).Close()

                End If

                'Dim logFile As String = "App_Data/ErrorLog.txt"

                logFile = HttpContext.Current.Server.MapPath(logFile)

                ' Open the log file for append and write the log

                Dim sw As New StreamWriter(logFile, True)

                sw.Write("******************** " & DateTime.Now)

                sw.WriteLine(" ********************")

                If Not exc.InnerException Is Nothing Then

                    sw.Write("Inner Exception Type: ")

                    sw.WriteLine(exc.InnerException.[GetType]().ToString())

                    sw.Write("Inner Exception: ")

                    sw.WriteLine(exc.InnerException.Message)

                    sw.Write("Inner Source: ")

                    sw.WriteLine(exc.InnerException.Source)

                    If Not exc.InnerException.StackTrace Is Nothing Then

                        sw.WriteLine("Inner Stack Trace: ")

                    End If

                    sw.WriteLine(exc.InnerException.StackTrace)

                End If

                sw.Write("Exception Type: ")

                sw.WriteLine(exc.[GetType]().ToString())

                sw.WriteLine("Exception: " & exc.Message)

                sw.WriteLine("Source: " & source)

                sw.WriteLine("Stack Trace: ")

                If Not exc.StackTrace Is Nothing Then

                    sw.WriteLine(exc.StackTrace)

                End If

                sw.WriteLine()

                sw.Close()

            Catch ex As Exception

            End Try

           

        End Sub

        ' Notify System Operators about an exception

        Public Shared Sub NotifySystemOps(ByVal exc As Exception)

            ' Include code for notifying IT system operators

        End Sub

    End Class

    方法二 Application_Error,拦截异常

        Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

            Dim exc As Exception = Server.GetLastError()

           

            ' Log the exception and notify system operators

            ExceptionUtility.LogException(exc, "DefaultPage")

            ExceptionUtility.NotifySystemOps(exc)

                   

            Response.Write("<h2> Error</h2>" & vbLf)

            Response.Write("<font color=red>Contact to Administrator</font>" & vbLf)

            Response.Write("<p>" + exc.Message & "</p>" & vbLf)

            Response.Write("<p>" + exc.InnerException().ToString() & "</p>" & vbLf)

            ' Clear the error from the server

            Server.ClearError()

        End Sub

  • 相关阅读:
    InfluxDB执行语句管理(query management)
    InfluxDB数据备份和恢复方法,支持本地和远程备份
    WordPress多本小说主题–WNovel主题发布,十分钟搭建小说站! 现已更新至1.2版本
    InfluxDB安装后web页面无法访问的解决方案
    Influxdb原理详解
    InfluxDB学习之InfluxDB连续查询(Continuous Queries)
    InfluxDB学习之InfluxDB数据保留策略(Retention Policies)
    InfluxDB学习之InfluxDB的HTTP API查询操作
    玩游戏,王者荣耀,记录
    开源抓包工具PowerSniff(支持lua,c语言作为脚本实时分析)
  • 原文地址:https://www.cnblogs.com/htht66/p/1776021.html
Copyright © 2011-2022 走看看