Lm th? no ? n ngo?i l? trong Visual Basic 2005 ho?c trong Visual Basic.NET

D?ch tiu ? D?ch tiu ?
ID c?a bi: 301283 - Xem s?n ph?m m bi ny p d?ng vo.
?i v?i Microsoft Visual C# 2005 v Microsoft Visual C#.NET Phin b?n c?a bi vi?t ny, xem 308345.

? c m?t phin b?n Microsoft Visual Basic 6.0 c?a bi vi?t ny, xem 191474.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

nh b?t v ?i ph v?i ngo?i l? l m?t tiu chu?n l?p tr?nh cng vi?c. Bi vi?t ny ch?ng t? lm th? no ? s? d?ng m?t cu?i cng th?-b?t-? kh?i ? n m?t ngo?i l?. Mt cu?i cng th?-b?t-? kh?i l m?t wrapper"" m b?n ?t xung quanh b?t k? m? ni cc kh? nng x?y ra m?t ngo?i l? t?n t?i.

Mt cu?i cng th?-b?t-? kh?i bao g?m nh?ng ph?n sau:
  • B?t k? m? c th? nm m?t ngo?i l? ?c ?t bn trong cc H?y th? kh?i.
  • N?u m?t ngo?i l? ?c nm, cc b?t kh?i ?c nh?p vo, v chng tr?nh c th? th?c hi?n nhan chi?n d?ch ? ph?c h?i ho?c c?nh bo ng?i s? d?ng.
  • M? s? trong cc cu?i cng kh?i lun lun b? hnh quy?t v c th? th?c hi?n d?n s?ch sau khi m?t ngo?i l? ? x?y ra. Cc cu?i cng kh?i l ty ch?n.

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 d?ch v? m b?n c?n:
  • Microsoft Windows Server 2003, Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, ho?c my ch? Microsoft Windows NT 4.0
  • Microsoft Visual Basic 2005 ho?c Microsoft Visual Basic.NET

V d?-by-step

  1. T?o m?t ?ng d?ng giao di?n i?u khi?n Visual Basic 2005 ho?c trong Visual Basic.NET.
  2. Trong th? t?c ph? chnh, chn m? m?u m sau. Cc m?u tuyn b? v kh?i ba bi?n, m trong cc kh?i t?o "k" gy ra m?t l?i.
    ' This code generates an exception.
    	Console.WriteLine("We're going to divide 10 by 0 and see what happens...")
    	Console.WriteLine()
    
    	Dim i as Integer = 10
    	Dim j as Integer = 0
    	Dim k As Integer = i/j ' Error on this line.
    					
  3. B?m phm F5 ? xem l ngo?i l?.
  4. Qu?n m?t c? g?ng n?m b?t tuyn b? trn m? c?a b?n ? n?m b?t cc l?i. Sau y M? b?t t?t c? cc l?i ?c nm ra trong m? v hi?n th? m?t chung thng bo l?i. Thay th? m? trong th? t?c ph? chnh v?i sau M?.
    ' This code shows how to catch an exception.
    Try
    	Console.WriteLine("We're going to divide 10 by 0 and see what happens...")
    	Console.WriteLine()
    
    	Dim i as Integer = 10
    	Dim j as Integer = 0
    	Dim k As Integer = i/j ' Error on this line. Control jumps to the catch block.
    Catch
    	Console.WriteLine("An error occurred.")
    End Try
  5. Nh?n CTRL + F5 ? ch?y cc d? n. Nh?n th?y r?ng cc l?i thng i?p t? cc b?t kh?i s? ?c hi?n th?.
  6. N?u m?t s? d?n d?p ho?c ch? bi?n c?n ph?i lm b?t k? c?a m?t l?i, s? d?ng cc cu?i cng l m?t ph?n c?a cc cu?i cng th?-b?t-? tuyn b?. M? s? trong cc cu?i cng m?t ph?n c?a cc tuyn b? l lun lun b? hnh quy?t, b?t k? c?a m?t ngo?i l?. o?n m? sau hi?n th? "tuyn b? ny l lun lun ?c in" trong bn i?u khi?n, ngay c? khi khng c l?i x?y ra. Thay th? m? ph? chnh v?i cc m? sau y.
    ' This code shows how to catch an exception.	
    Try
    	Console.WriteLine("We're going to divide 10 by 0 and see what happens...")
    	Console.WriteLine()
    
    	Dim i as Integer = 10
    	Dim j as Integer = 0
    	Dim k As Integer = i/j ' Error on this line. Control jumps to the catch block.
    
    Catch
    	Console.WriteLine("An error occurred.")
    Finally   ' This section is performed regardless of the above processing.
    	Console.WriteLine()
    	Console.WriteLine("This statement is always printed")
    End Try
    					
  7. Nh?n CTRL + F5 ? ch?y cc d? n.
  8. B?n c th? s? d?ng cc ?i t?ng ngo?i l? v?i cc b?t tuyn b? ? l?y thng tin chi ti?t v? ngo?i l?. M?t ngo?i l? c m?t s? ti s?n c th? gip b?n xc ?nh gip, ngu?n, ho?c th?m ch ngn x?p thng tin v? m?t ngo?i l?. Thng tin ny c th? r?t h?u ch cho theo d?i xu?ng nguyn nhn g?c c?a ngo?i l? ho?c cung c?p m?t t?t hn gi?i thch v? ngu?n g?c c?a n. Cc m?u sau b?t m?t ngo?i l? v cho m?t thng bo l?i c? th?. Nh tr?c, b?n c th? thay th? m? trong chnh Sub th? t?c v?i o?n m? sau.

    V d? 1
    ' This code shows how to catch an exception.
    Try
    	Console.WriteLine("We're going to divide 10 by 0 and see what happens...")
    	Console.WriteLine()
    
    	Dim i as Integer = 10
    	Dim j as Integer = 0
    	Dim k As Integer = i/j ' Error on this line. Control jumps to the catch block.
    
    Catch e As Exception  ' Perform code that deals with the exception
                          ' or informs the user what occurred.
    	Console.WriteLine("The following error occurred:")
    	Console.WriteLine(e.ToString())  ' Print the error message to the user.
    
    Finally   ' This section is performed regardless of the above processing.
    	Console.WriteLine()
    	Console.WriteLine("This statement is always printed")
    End Try
    					
    V d? 2
    ' This code shows how to catch an exception.
    Try
    	Console.WriteLine("We're going to divide 10 by 0 and see what happens...")
    	Console.WriteLine()
    
    	Dim i as Integer = 10
    	Dim j as Integer = 0
    	Dim k As Integer = i/j ' Error on this line. Control jumps to the catch block.
    
    Catch e As Exception  ' Perform code that deals with the exception
                          ' or informs the user what occurred.
            Console.WriteLine("The following error occurred:")
            Console.WriteLine( e.Message )     ' Print the error message.
            Console.WriteLine( e.Source )      ' Name of application or object
                                               ' that caused the error
            Console.WriteLine( e.StackTrace )  ' String that contains the stack 
                                               ' trace for this exception
    Finally   ' This section is performed regardless of the above processing.
            Console.WriteLine()
            Console.WriteLine("This statement is always printed")
    End Try
    					
  9. Cho ?n th?i i?m ny, b?n c x? l? v?i tr?ng h?p c? th? c?a m?t ngo?i l? khng c? th?. Tuy nhin, n?u b?n bi?t tr?c nh?ng g? lo?i ngo?i l? s? x?y ra, b?n c th? b?t ngo?i l? d? on v qu tr?nh cho ph h?p. Sau , b?n c th? dng nhi?u B?t kh?i ? n?m b?t t?t c? cc tr?ng h?p ngo?i l? v ?i ph v?i h? l t?t. Cc m?u sau ch?ng t? i?u ny.
    Module Module1
    Sub Main()
    
    ' Attempt to do some file processing.
    ' This can cause a number of potential exceptions.
    ' Note that you can use the specific exceptions to get extra information about that exception,
    ' but this example focuses on showing control execution for those exceptions.
    
    Try
    	File.Create("c:/temp/test.txt")  ' Can fail for a number of reasons
    
    Catch ioe As System.IO.IOException ' This error may occur if the Temp folder does not exist.
    	Console.WriteLine("An IO error occurred. The c:\temp folder does not exist")
    
    Catch se As System.Security.SecurityException ' You do not have the appropriate permission
                                                  ' to take this action.
    	Console.WriteLine("You don't have the security permissions to take this action!")
    
    Catch e As Exception    ' Catch all other exceptions.
    	Console.WriteLine( e.ToString() )  ' Print the standard exception information.
    End Try
    End Sub
    End Module
    B?i v? my tnh c?u h?nh c th? khc nhau, cc m?u ? y b?c c th? ho?c c th? khng nm m?t ngo?i l?. N?u b?n mu?n p bu?c m?t ngo?i l? IO, thay ?i ?ng d?n c?a t?p vo m?t th m?c m khng c trn c?a b?n my tnh.

THAM KH?O

? bi?t thm chi ti?t, xem Microsoft.NET Framework Ph?n m?m b? pht tri?n (SDK).

Thu?c tnh

ID c?a bi: 301283 - L?n xem xt sau cng: 26 Thang Tam 2011 - 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 KB301283 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:301283

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