Differences
This shows you the differences between the selected revisions of the page.
2012-03-20 | 2012-03-20 | ||
option explicit (martin) | error handling in vbscript (martin) | ||
Line 70: | Line 70: | ||
Set session = WScript.CreateObject("WinSCP.Session", "session_") | Set session = WScript.CreateObject("WinSCP.Session", "session_") | ||
+ | </code> | ||
+ | |||
+ | ==== [[vberror]] Error Handling in VBScript ==== | ||
+ | VBScript 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 aborting the script on error (the default for WSH), use ''[[http://msdn.microsoft.com/en-us/library/53f3k80h(v=vs.85).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/sbf5ze0e(v=vs.85).aspx|Err.Number]]'' after every statement to test for errors. You can revert to default error handling (aborting the script) 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 | ||
+ | session.Open sessionOptions | ||
+ | |||
+ | ' Query for errors | ||
+ | If Err.Number <> 0 Then | ||
+ | WScript.Echo "Error opening session: " & Err.Description | ||
+ | WScript.Quit 1 | ||
+ | End If | ||
+ | |||
+ | ' Clear the error | ||
+ | Err.Clear | ||
+ | |||
+ | ' Restore default error handling | ||
+ | On Error GoTo 0 | ||
+ | |||
+ | ' Now, with default error handling restored, | ||
+ | ' script aborts, if reading remote directory fails | ||
+ | Dim directoryInfo | ||
+ | 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: | ||
+ | |||
+ | <code vb> | ||
+ | Sub ListDirectory | ||
+ | ... | ||
+ | session.Open sessionOptions | ||
+ | |||
+ | Dim directoryInfo | ||
+ | Set directoryInfo = session.ListDirectory("/home/user/") | ||
+ | ... | ||
+ | End Sub | ||
+ | |||
+ | ' 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 script) | ||
+ | ListDirectory | ||
+ | |||
+ | ' Query for errors | ||
+ | If Err.Number <> 0 Then | ||
+ | WScript.Echo "Listing directory failed: " & Err.Description | ||
+ | WScript.Quit 1 | ||
+ | End If | ||
+ | |||
+ | ' Clear the error | ||
+ | Err.Clear | ||
+ | |||
+ | ' Restore default error handling | ||
+ | On Error GoTo 0 | ||
</code> | </code> | ||
Line 115: | Line 180: | ||
===== [[vbscript]] VBScript Example ===== | ===== [[vbscript]] VBScript Example ===== | ||
- | This example is functionally equivalent to [[library#example|overall C# example for WinSCP .NET assembly]]. | + | This example is functionally equivalent to [[library#example|overall C# example for WinSCP .NET assembly]], except for [[library_com_wsh#vberror|error handling]]. |
There are also [[library_examples|other VBScript examples]]. | There are also [[library_examples|other VBScript examples]]. |