vb.net编写自定义异常

来源:互联网 发布:江汉大学网络 编辑:程序博客网 时间:2024/04/28 11:16

要创建一个自定义的异常,你可以自定义一个异常类。为了确保它是一个 .Net 的异常,你的异常类应该从一个 .Net 异常类中继承而来。其中一个比较好的继承对象是
ApplicationException 类。就像下面例子中的UsernameNotFoundException类:

Public Class UsernameNotFoundException : Inherits ApplicationException
   Public Sub New()
      MyBase.New()
   End Sub
   Public Sub New(ByVal message As String)
      MyBase.New(message)
   End Sub
   Public Sub New(ByVal message As String, ByVal innerEx As Exception)
      MyBase.New(message, innerEx)
   End Sub
End Class

使用继承的时候,新的类会自动从父类继承了所有的属性。因此,UsernameNotFoundException 类会拥有 ApplicationException 类的所有属性,例如消息和调用。
新类不会继承父类的参数表,因此你需要为新的类定义参数表。ApplicationException 支持以下三种参数结构。

•One with no parameters  没有参数
•One with just the message parameter 只有一个消息参数
•One with both a message and an inner exception 一个消息和一个内部的异常

 

最后一个参数结构是用于代码截获一个异常后再重新丢出一个异常,但又想保持原有的异常信息。

同样地你可以为你的新异常自定义属性和事件,例如,你可以为你的异常加入一个 username 属性用来记录所有产生异常的用户的信息。
丢出自定义异常的代码如下:

Public Function ValidateLogin(ByVal sUserName As String, _
      ByVal sPassword As String) As Boolean
   If sUserName.length=0 OrElse sPassword.Length=0 Then
      Throw New ArgumentOutOfRangeException("Username and password are required.")
   End If
   '' Code to locate the user record here
   If iUserRecordID = 0 Then
      Throw New UsernameNotFoundException("Invalid username")
   End If
   '' Code to retrieve the password from the user record here
   If sPassword <> sUserRecordPassword Then
      Throw New PasswordInvalidException("Invalid password")
   End If
   Return True
End Function

Private Sub cmdLogin_Click(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles cmdLogin.Click
   Dim oUser As User()
   Dim bValid as Boolean
 Try
   oUser = New User()
   bValid = oUser.ValidateLogin(txtUserName.Text, txtPassword.Text)
   If bValid then
      DialogResult = DialogResult.OK
   End If
 Catch ex As UsernameNotFoundException
   MessageBox.Show(ex.Message)
   txtUserName.Focus()
 Catch ex As PasswordInvalidException
   MessageBox.Show(ex.Message)
   txtPassword.Focus()
 Catch ex As Exception
   MessageBox.Show(ex.Message)
 Finally
   oUser.Dispose()
   oUser = Nothing
 End Try
End Sub

异常的参数是很重要的,一些特别的参数通常要在一般的异常参数之前定义。一般的异常参数通常都放到最后以便能截住所有的不可预测异常。

 

 

自己写的:

 Public Class myexception : Inherits ApplicationException
    Public Sub New(ByVal message As String)
        MyBase.New(message)
    End Sub
End Class

 

 

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Try
            Dim Path As String = "c:/abc.txt"

            If Directory.Exists(Path) = False Then
                Throw New myexception("error")
            End If
            Dim fs As FileStream = New FileStream("Path", FileMode.Open, FileAccess.ReadWrite, FileShare.Read)
            Dim a As TextReader
            Dim b(1024) As Byte
            fs.Read(b, 0, b.Length)
            Dim temp As UTF8Encoding = New UTF8Encoding(True)

            MsgBox(String.Format(temp.GetString(b)))


        Catch ex As myexception
            MessageBox.Show(ex.Message)
        Finally
            MsgBox("heh")

        End Try

    End Sub
End Class