异常处理點滴
异常处理重要
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