Differences

This shows you the differences between the selected revisions of the page.

2012-03-21 2012-03-22
example anchor (martin) copying error handling section from vbscript (martin)
Line 40: Line 40:
    MsgBox e.Filename & " => " & e.Destination     MsgBox e.Filename & " => " & e.Destination
End Sub End Sub
 +</code>
 +
 +==== [[error]] Error Handling ====
 +VBA does not support catching exceptions, what is a common way of handling errors in examples for most other languages.
 +
 +In case you need to use custom error handling, instead of interrupting a VB macro (the default behaviour), use ''[[http://msdn.microsoft.com/en-us/library/gg251688.aspx|On Error]]'' statement.
 +
 +Use ''On Error Resume Next'' to disable default error handling. Then you need to query ''[[http://msdn.microsoft.com/en-us/library/gg251525.aspx|Err.Number]]'' after every statement to test for errors. You can revert to default error handling (aborting the macro) using ''On Error GoTo 0''.
 +
 +<code vb>
 +' Enable custom error handling
 +On Error Resume Next
 +
 +' Now, with custom error handling enabled,
 +' script does not abort, when opening session fails
 +mySession.Open sessionOptions
 +
 +' Query for errors
 +If Err.Number <> 0 Then
 +    MsgBox "Error opening session: " & Err.Description
 +    End
 +End If
 +
 +' Restore default error handling
 +On Error GoTo 0
 +
 +' Now, with default error handling restored,
 +' script aborts, if reading remote directory fails
 +Dim directoryInfo As RemoteDirectoryInfo
 +Set directoryInfo = session.ListDirectory("/home/user/")
 +</code>
 +
 +If you do not want to test for errors after every statement, you need to group the staments you want to guard into a subprocedure and enable custom error handling before calling/entering the subprocedure.
 +
 +This approach is also recommended to ensure that ''[[library_session_dispose|Session.Dispose]]'' is called even in case of error.
 +
 +<code vb>
 +Sub ListDirectory(ByRef mySession As Session)
 +    ' Setup session options
 +    ...
 +
 +    ' Connect
 +    mySession.Open sessionOptions
 +
 +    Dim directoryInfo As RemoteDirectoryInfo
 +    Set directoryInfo = session.ListDirectory("/home/user/")
 +
 +    ' Do some stuff with directory listing
 +    ...
 +End Sub
 +
 +Dim mySession As New Session
 +
 +' Enable custom error handling
 +On Error Resume Next
 +
 +' Now, with custom error handling enabled before calling the ListDirectory subprocedure,
 +' any statement in the subprocedure terminates the subprocedure (but not the whole execution)
 +ListDirectory mySession
 +
 +' Query for errors
 +If Err.Number <> 0 Then
 +    MsgBox "Listing directory failed: " & Err.Description
 +
 +    ' Disconnect, clean up
 +    session.Dispose
 +
 +    End
 +End If
 +
 +' Disconnect, clean up
 +mySession.Dispose
 +
 +' Restore default error handling
 +On Error GoTo 0
 +
 +' Now with session cleanly closed, safely do anything unrelated to WinSCP session
 +...
</code> </code>
Line 53: Line 131:
       
    On Error Resume Next     On Error Resume Next
-     
-    mySession.DisableVersionCheck = True 
       
    Upload mySession     Upload mySession
Line 61: Line 137:
    If Err.Number <> 0 Then     If Err.Number <> 0 Then
        MsgBox "Error: " & Err.Description         MsgBox "Error: " & Err.Description
 +
 +        ' Clear the error
 +        Err.Clear
    End If     End If
       
-    ' Clear the error 
-    Err.Clear 
-         
    ' Disconnect, clean up     ' Disconnect, clean up
    mySession.Dispose     mySession.Dispose

Last modified: by martin