Lm th? no ? s? d?ng c?u trc ngo?i l? x? l? trong Visual Basic.NET hay Visual Basic 2005

D?ch tiu ? D?ch tiu ?
ID c?a bi: 315965 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t ny ch?ng t? lm th? no ? s? d?ng c?u trc ngo?i l? x? l? trong Visual Basic.NET hay Visual Basic 2005.

Yu c?u

Danh sch sau v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, c s? h? t?ng m?ng v gi b?n ghi d?ch v? m b?n c?n:
  • Visual Basic.NET ho?c Visual Basic 2005
Bi vi?t ny gi? ?nh r?ng b?n ? quen thu?c v?i cc ch? ? sau:
  • Visual Basic.NET ho?c Visual Basic 2005
  • Microsoft Visual Studio.NET ho?c Microsoft Visual Studio 2005

C?u trc ngo?i l? x? l?

Visual Basic.NET ho?c Visual Basic 2005 cung c?p c?u trc ngo?i l? x? l? m cung c?p m?t thay th? m?nh m?, d? ?c hn ? qu?n l? l?i "On Error Goto", m l c s?n trong phin b?n tr?c c?a Microsoft Visual Basic. X? l? c c?u trc ngo?i l? l m?nh hn b?i v? n cho php b?n ? lm t? l?i x? l? bn trong cc b? x? l? l?i trong cc th? t?c tng t?. Hn n?a, c c?u trc ngo?i l? x? l? s? d?ng m?t c php kh?i tng t? nh n?u...Khc...K?t thc n?u tuyn b?. i?u ny lm cho Visual Basic.NET v Visual Basic 2005 m? d? ?c hn v d? dng hn ? duy tr?.

Lu ?: Visual Basic.NET ho?c Visual Basic 2005 gi? l?i c php "On Error Goto" cho tng h?p v? sau ng?c. B?n v?n c th? s? d?ng c php ny trong Visual Basic.NET hay Visual Basic 2005 m?. Tuy nhin, b?n khng th? k?t h?p x? l? ngo?i l? c c?u trc v pht bi?u V? l?i trong cc th? t?c tng t?. Khi b?n vi?t m? m?i, Microsoft khuy?n co b?n s? d?ng c?u trc x? l? ngo?i l?.

C php c b?n c?a c?u trc l?i x? l? l nh sau:
Try
   'Code that may raise an error.
Catch
   'Code to handle the error.
Finally
   'Code to do any final clean up.
End Try
				
H?y th? v K?t thc th? pht bi?u ?c yu c?u. Nh?ng i?u kho?n b?t v cu?i cng l khng c?n thi?t, m?c d b?n ph?i bao g?m t nh?t m?t trong s? h? trong m? c?a b?n. B?n c?ng c th? xc ?nh nhi?u b?t cu ? cho m?i kh?i b?t x? l? m?t l?i c? th?.

B?t m?t ngo?i l?

  1. B?t ?u Visual Studio.NET ho?c Visual Studio 2005.
  2. T?o m?t d? n m?i giao di?n i?u khi?n ?ng d?ng trong Visual Basic.NET hay Visual Basic 2005.
  3. Trong th? t?c Ph? chnh , thm m? sau y:
    Dim a As Integer = 0
    Dim b As Integer = 0
    Dim c As Integer = 0
    
    Try
      a = b \ c
    Catch exc As Exception
      Console.WriteLine("A run-time error occurred")
    Finally
      Console.ReadLine()
    End Try
    					
  4. Trn tr?nh n g? l?i , nh?p B?t ?u ? ch?y cc ?ng d?ng. M? c? g?ng chia m?t s? zero. y l m?t ho?t ?ng b?t h?p php m gy ra m?t phn chia b?ng s? khng l ngo?i l?. May m?n thay, kh?i b?t b?t l?i ny, v c?a s? giao di?n i?u khi?n s? hi?n th? thng bo l?i sau:
    A run-time error has occurred
    						
  5. ng c?a s? giao di?n i?u khi?n.

B?t nhi?u tr?ng h?p ngo?i l?

Ph?n ny ch?ng t? lm th? no ? s? d?ng nhi?u b?t cu ? x? l? cc l?i khc nhau.
  1. M? d? n ?ng d?ng giao di?n i?u khi?n m b?n ? t?o trong ph?n tr?c.
  2. Trong th? t?c Ph? chnh , thay th? m? m b?n ? sao chp trong ph?n tr?c v?i o?n m? sau:
    Dim a As Integer = 2147483647
    Dim b As Integer = 0
    Dim c As Integer = 0
    
    Try
      a += 1
    Catch exc As DivideByZeroException
      Console.WriteLine("Error: Divide by zero")
    Catch exc As OverflowException
      Console.WriteLine("Error: Overflow")
    Finally
      Console.ReadLine()
    End Try
    						
    M? ny bao g?m hai b?t kh?i: m?t ? n s? phn chia tr?c b?ng s? khng l?i, v m?t ? b?t l?i trn m?i.
  3. Trn tr?nh n g? l?i , nh?p B?t ?u ? ch?y cc ?ng d?ng. C?a s? giao di?n i?u khi?n s? hi?n th? thng bo l?i sau:
    Error: Overflow
    						
  4. ng c?a s? giao di?n i?u khi?n.
  5. B?i v? b?n lun lun khng th? d? on m?i l?i c th? x?y ra, b?n c th? thm m?t n?m b?t t?t c? cc kh?i cho tr?ng h?p ngo?i l? unanticipated. V d?, thm m? sau y tr?c khi tuyn b? cu?i cng ? n?m b?t b?t k? l?i unanticipated v hi?n th? cc thng bo l?i thch h?p:
    Catch exc As Exception
    Console.WriteLine("Error: " & exc.Message)
    					
  6. Trn menu t?p , b?m vo Gi?i php ng.

Nm ngo?i l?

C?u trc ngo?i l? x? l? s? d?ng cc bo co b?t ? n m?t ngo?i l?. C?u trc ngo?i l? x? l? c?ng cung c?p m?t cch ? nm m?t ngo?i l?. V d?, n r?t h?u ch ? nm m?t ngo?i l? khi b?n th?c hi?n kim soat d liu bn trong m?t B?t ?ng s?n ?t th? t?c b?i v? b?n c th? mu?n nm m?t l?i n?u m?t quy t?c kinh doanh l vi ph?m.
  1. B?t ?u Visual Studio.NET ho?c Visual Studio 2005.
  2. T?o m?t d? n m?i giao di?n i?u khi?n ?ng d?ng trong Visual Basic.NET hay Visual Basic 2005.
  3. Trn tr?nh n d? n , nh?p Thm l?p.
  4. Trong c?a s? Add New Item , g? clsPerson.vb trong tn h?p vn b?n, v sau b?m OK.
  5. Thm m? sau bn trong cng c?ng l?p clsPerson...K?t thc l?p pht bi?u:
    Private mintAge As Integer
    
    Public Property Age() As Integer
      Get
        Age = mintAge
      End Get
      Set(ByVal Value As Integer)
        If Value >= 0 Then
          mintAge = Value
        Else
          Throw New ArgumentException ("Age cannot be negative")
        End If
      End Set
    End Property
    						
    M? ny t?o ra m?t ti s?n tu?i . B?i v? m?t ng?i khng th? c m?t ? tu?i tiu c?c, m?t l?i ?c a ln n?u ng?i s? d?ng l?p c? g?ng thi?t l?p tu?i v?i m?t s? l t hn zero.
  6. Trong th? t?c Ph? chnh c?a Module1.vb, thm m? sau y:
    Dim p As New clsPerson()
    
    Try
      p.Age = -1
    Catch exc As Exception
      Console.WriteLine(exc.Message)
    Finally
      Console.ReadLine()
    End Try
    					
  7. Trn tr?nh n g? l?i , nh?p B?t ?u ? ch?y cc ?ng d?ng. C?a s? giao di?n i?u khi?n s? hi?n th? thng bo l?i sau:
    Age cannot be negative
    						
  8. ng c?a s? giao di?n i?u khi?n.

Danh sch m? ?y ?

B?t m?t ngo?i l?

Module Module1

  Sub Main()
    Dim a As Integer = 0
    Dim b As Integer = 0
    Dim c As Integer = 0

    Try
      a = b \ c
    Catch exc As Exception
      Console.WriteLine("A run-time error occurred")
    Finally
      Console.ReadLine()
    End Try

  End Sub

End Module
				

B?t nhi?u tr?ng h?p ngo?i l?

Module Module1

  Sub Main()
    Dim a As Integer = 2147483647
    Dim b As Integer = 0
    Dim c As Integer = 0

    Try
      a += 1
    Catch exc As DivideByZeroException
      Console.WriteLine("Error: Divide by zero")
    Catch exc As OverflowException
      Console.WriteLine("Error: Overflow")
    Catch exc As Exception
      Console.WriteLine("Error: " & exc.Message)
    Finally
      Console.ReadLine()
    End Try


  End Sub

End Module
				

Nm ngo?i l?

Module Module1

  Sub Main()
    Dim p As New clsPerson()

    Try
      p.Age = -1
    Catch exc As Exception
      Console.WriteLine(exc.Message)
    Finally
      Console.ReadLine()
    End Try
  End Sub

End Module

Public Class clsPerson

  Private mintAge As Integer

  Public Property Age() As Integer
    Get
      Age = mintAge
    End Get
    Set(ByVal Value As Integer)
      If Value >= 0 Then
        mintAge = Value
      Else
        Throw New ApplicationException("Age cannot be negative")
      End If
    End Set
  End Property

End Class
				

TI LI?U THAM KH?O

? bi?t thm chi ti?t, ch? ? sau Microsoft web site web:
L?i x? l? VB.NET cch: S?ng v?i tr?ng h?p ngo?i l? (m?t Nh pht tri?n Visual Basic bi)
http://MSDN.Microsoft.com/en-US/Library/aa227603 (v=vs.60) .aspx

Lm th? no ti b?t m?t ngo?i l?
http://MSDN.Microsoft.com/en-US/Library/ys1b32h3 (v=vs.80) .aspx

Lm th? no ti nm m?t ngo?i l?
http://MSDN.Microsoft.com/en-US/Library/z7w9ff54 (v=vs.80) .aspx

Thu?c tnh

ID c?a bi: 315965 - L?n xem xt sau cng: 15 Thang Sau 2012 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
T? kha:
kbvs2005swept kbvs2005applies kbhowtomaster kbmt KB315965 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny: 315965

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com