I am trying to modify a vbscript and convert it to Powershell as instructed. I have a block of code on my function SearchAD with On Error.
on error resume ne
Simply put, On Error Resume Next
disables error reporting. And On Error GoTo 0
restores it. It's often used when a programmer expects an error to occur but doesn't want that error to go unhandled and halt their script. For example:
' This statement will cause VBScript to throw an error and halt.
i = 1 / 0
' Instead, let us handle the error and decide if its important...
On Error Resume Next ' Turn off error reporting
i = 1 / 0
' Now, we can test the Err object to see if any errors were thrown...
If Err.Number = 0 Then
' Success. No error occurred.
ElseIf Err.Number = 11 Then
' Error #11 is 'Division by zero'. Do we want to allow it?
End If
' We're through the risky section. Restore error reporting.
On Error GoTo 0
Now time for a soap box rant. Don't use it. There are almost always better ways than using On Error Resume Next
. Assert variable values. Check your array bounds before trying to access array elements. Do QA testing. Validate user input. Be a good programmer and cover all your angles. Don't just close your eyes to any errors and assume everything is going to work. It's abused all too often by VB beginners and, unfortunately, even some of the experts! (end rant)