2012年2月6日月曜日

[VBA] On Error Go To

Amazed the fact that VBA still holds "On Error GoTo" statement, which is unstructured and came from an ice age.
//http://excelvba.pc-users.net/fol6/6_8.html

Sub OnErrorTest()
    On Error GoTo ErrorTrap
    
    Dim i As Integer
    i = "test"             '
    
    MsgBox "Exit."
    
    Exit Sub

ErrorTrap:
    MsgBox "Error no.:" & Err.Number
    MsgBox "Description:" & Err.Description
    MsgBox "Help file" & Err.HelpContext
    MsgBox "Project:" & Err.Source
    Resume Next
End Sub
//

Now I feel how modern "try/catch" statement is sophisticated.

0 件のコメント:

コメントを投稿