Cannot trap 8004020f error in VBScript - vbscript

I cannot trap the 8004020f error generated by a script in Visual Basic (CDOSYS component).
I used the "on error resume next", it traps all errors except for this one.
This error is generated in this situation:
- the mailserver I use for sending hosts mailboxes belonging to #mydomain.abc
- I try to send mail to nonexistant#mydomain.abc
That is, if I try to send an email to a non-existant user of a domain hosted on the same mailserver used for sending, this action causes the error.
This is (almost) impossible to catch!! What can I do? I have a lot of databases and my old non-existant address is found in hundredths of tables and databases!
Thanks

Put in the following in order to find the real error code, which is a decimal number. For example, 13=type mismatch.
<%
response.write("err.number=" & err.number & "<br>")
%>
OR...
I looked at my own stuff and found this:
Select Case err.Number
Case -2147220973
strRet = " Failure to Send Report Message - Server Not Found" & vbCrLf & " Error: " & err.Number & " - " & err.Description
Case -2147220975
strRet = " Failure to Send Report Message - Server Authentication Failed" & vbCrLf & " Error: " & err.Number & " - " & err.Description
Case Else
strRet = " Failure to Send Report Message - Error: " & err.Number & " - " & err.Description
End Select

Related

If we face more than one error while using On Error Resume Next

While using On Error Resume Next,it skips the encountered error and move to the next line.
If we use err.Number and err.Description it shows message and number related to the error.
My question is: what if it faces more than on error...then how it will show?
On Error Resume Next
intDivideByZero
floatDivideByZero
If err.Number <> 0 Then
msgbox "the error number is : " & err.Number
msgbox "the error description is : " & err.Description
End If
On error Resume 0
VBScript error handling is rather limited. You will need to put an error handler after each line where an error can occur. Also, the Err object isn't automatically reset after an error, so you need to do that yourself. Otherwise the object will still indicate an error after the next statement, even if none occured there.
On Error Resume Next
intDivideByZero
If Err Then
WScript.Echo "0x" & Hex(Err.Number) & ": " & Err.Description
Err.Clear
End If
floatDivideByZero
If Err Then
WScript.Echo "0x" & Hex(Err.Number) & ": " & Err.Description
Err.Clear
End If
On Error Goto 0
You can simplify that a little bit by wrapping the handler in a procedure and calling that procedure after each statement:
Sub Trap
If Err Then
WScript.Echo "0x" & Hex(Err.Number) & ": " & Err.Description
Err.Clear
End If
End Sub
On Error Resume Next
intDivideByZero : Trap
floatDivideByZero : Trap
On Error Goto 0

VbScript Error Object Cleared by On Error Statement

Kind of a novice with VbScript, and trying to implement error handling. My method is to pass the error object to a HandleErr sub, but the error apparently gets cleared by the "On Error Resume Next" statement withing the sub. Using Windows 7.
On Error Resume Next
Dim x
x = 1/0
msgbox "Original Error: " & err.Number & " - " & err.Description
if err.number <> 0 then HandleErr err
Sub HandleErr(objErr)
on error resume next '### Without this On Error statement, the script runs fine.
msgbox "Error in HandleErr: " & objErr.Number & " - " & objErr.Description '### objErr.Number becomes zero.
WScript.Quit objErr.Number
End Sub
I imagine there is a simple answer for this. Any help would be greatly appreciated.
You want to stop the skipping errors with On Error Resume Next once you reach HandleErr(). Also use Err.Clear() to reset Err object.
On Error Resume Next
Dim x
x = 1/0
MsgBox "Original Error: " & Err.Number & " - " & Err.Description
if Err.Number <> 0 then HandleErr Err
'Stop skipping lines when errors occur.
On Error Goto 0
Sub HandleErr(objErr)
MsgBox "Error in HandleErr: " & objErr.Number & " - " & objErr.Description '### objErr.Number becomes zero.
'Clear current error now you have trapped it.
Err.Clear
WScript.Quit objErr.Number
End Sub
Personally though I wouldn't pass Err into your function because Err is a global built-in object so you can still check the values without passing it in.
On Error Resume Next
Dim x
x = 1/0
MsgBox "Original Error: " & Err.Number & " - " & Err.Description
Call HandleErr()
'Stop skipping lines when errors occur.
On Error Goto 0
Sub HandleErr()
'Do we need to trap an error?
If Err.Number <> 0 Then
MsgBox "Error in HandleErr: " & Err.Number & " - " & Err.Description '### Err.Number becomes zero.
'Clear current error now you have trapped it.
Err.Clear
WScript.Quit Err.Number
End If
End Sub

Issues in Error handling sub in VB Script

I have written an error handling sub in my vb script
errorNumber = DoAllWork
Sub ErrorHandling (Number, Description, i)
If Number <> 0 Then
WriteLogFileLine logfile, "Error No : " & Number & " - " & Description & " has occurred !"
Else
WriteLogFileLine logfile, "Success copying files as Err.Number : " & Err.Number & "Total " & i & " files were copied ! " & vbcrlf
End If
Err.Clear
End Sub
And I am calling it in my vb script like this
Function DoAllWork
On Error Resume Next
Err.Clear
Do Until CopyFiles.AtEndOfStream
line = CopyFiles.ReadLine
For Each line In CopyFiles
If objFSO.GetFolder(line).Files.Count <> 0 then
WriteLogFileLine logfile, "Copying files FromLocation " & Chr(34) & line & Chr(34) & " to ToLocation " & Chr(34) & ToLocation & Chr(34)
Else
WriteLogFileLine logfile, "No files present in the folder " & Chr(34) & line & Chr(34) & vbcrlf
End if
i=0
For Each File In objFSO.GetFolder(line).Files
objFSO.GetFile(File).Copy ToLocation & "\" & objFSO.GetFileName(File),True
i=i+1
Next
ErrorHandling Err.Number, Err.Description, i
Next
Loop
End Function
Now the log file which is getting created has this error messages logged in it even though the files has got copied successfully. Can someone please suggest what is wrong with this error handling technique ??
2015-12-15 15:03:47 - Copying files FromLocation "\\srv10219\archive\Article\20151116_073104" to ToLocation "C:\Users\TEMPPAHIR\LearnVB\ICCdata\Article"
2015-12-15 15:03:47 - Error No : 438 - Object doesn't support this property or method has occurred !
when I place this error handling directly after the File.copy statement, it gives me such log..
2015-12-15 16:31:55 - Error No : 438 - Object doesn't support this property or method has occurred !
2015-12-15 16:31:55 - Success copying files as Err.Number : 0
2015-12-15 16:31:55 - Total 2 files were copied !
that means for the first file which is being copied it throws an error and for the second one it gives success even though both the files has been copied successfully
I implemented this to do pretty much what you asked. What the script needed to do was take a folder and its contents that was dropped into a directory, and then copy the contents to a new folder formatted correctly where there was a poller that picked up the files and entered them in our system. I needed the script to quit on any error, especially for the last thing to do. That was to delete the folder from the source directory, but I had to make sure it got copied correctly. (You could clear the error and continue as well.)
So after each important line like a folder create or a file copy I did this check. This worked for me to do error handling. Passing the Err object itself is the best way to go.
Set FolderCreate = FSO.CreateFolder(PreStagingDirectory + "\" + FolderCreateName)
ReportErrors Err,"Error creating folder: " + PreStagingDirectory + "\" + FolderCreateName
FSO.CopyFile objFile.Path, FolderCreate.Path + "\", true
ReportErrors Err,"Error copying file: " + objFile.Path + " to location: " + FolderCreate.Path
Sub ReportErrors(ErrorObject, strExtraInformation)
'This will log any errors if they happen and the script will then quit
If ErrorObject.Number <> 0 Then
OutPutFile.WriteLine(DateTimeString + " Error Number: " & ErrorObject.Number)
OutPutFile.WriteLine(DateTimeString + " Error (Hex): " & Hex(ErrorObject.Number))
OutPutFile.WriteLine(DateTimeString + " Source: " & ErrorObject.Source)
OutPutFile.WriteLine(DateTimeString + " Description: " & ErrorObject.Description)
OutPutFile.WriteLine(DateTimeString + " Other Information: " + strExtraInformation)
OutPutFile.WriteLine(DateTimeString + " Script is quitting due to the Error Condition.")
wscript.quit
End If
End Sub
I have also done code like this. In this case I needed to catch if null was returned from a SQL query to the database. If null was returned and I try to cast the value, it caused an error.
To answer the question of should you check for errors in a sub and clear them, or check for errors all through your script really depends what you need to accomplish.
First I have to say error handling in vbscipt just sucks. Using 'On Error Resume Next', is absolutely needed but be careful where you place it. I found it is not best to place it in main, at the top, just within each function. Most important is to remember the scope, if you put it in a function, you catch the error there, and can handle it. If you only have it at as the first line of your script, you can only handle an error in main. It is needed in main, sometimes.
Also to debug any vbscript, comment out any call to that, or you will never know the problem.
ExecutionString = "select Sum(cast(Frame_Count as int)) from Sop_Instance_T"
Set objRecordSet = objConnection.Execute(ExecutionString)
TotalFrames = cdbl(objRecordSet(0))
'In case null is returned catch the exception
'that happens on the above line
If Err.Number <> 0 Then
TotalFrames = 0
Err.Clear ()
End If

HTTP Status 443 on Server.CreateObject in VBScript under Classic ASP IIS 6.0

This is old legacy code that has been running at least 5 years. The DLL happens to relate to Paypal's PayFlowPro merchant processing service, but I think this is a Windows scenario causing the issue.
Suddenly, based on the code below, I'm seeing this error in the browser:
> Error with new credit card processing software, please call Neal at xxx-xxx-xxxx
> Error Ref102: client = Server.CreateObject returned Null
> (Detailed error: Object doesn't support this property or method)
> (Detailed error: 438)
The IIS log shows me the 443:
2013-12-19 00:57:24 W3SVC4 173.45.87.10 POST /myapps/adm_settle.asp - 443 - 76.187.77.159 Mozilla/5.0+(Windows+NT+6.2;+WOW64;+rv:26.0)+Gecko/20100101+Firefox/26.0 200 0 0
Since I saw the 433 above, I'm thinking there must be some security error. Just as a test, I tried putting the app-pool user in the Administrator group, restarted IIS, and still get exact same error. I have also given that user specific access to read the .DLL on disk.
I did run REGASM to try to re-register the .DLL. I also tried REGSRV32, but I guess that fails on .NET DLLs. It's been a few years since I've dealt with software this old.
The ASP/VBScript Code:
Err.Clear
On Error Resume Next
set client = Server.CreateObject("PayPal.Payments.Communication.PayflowNETAPI")
If Err.number > 0 Then
response.write "Error with new credit card processing software, please call Neal at xxx-xxx-xxxx"
response.write "</br>(Detailed error: " & Err.Description & ")"
response.write "</br>(Detailed error: " & Err.Number & ")"
response.End
End If
If client Is Nothing Then
Response.write "Error with new credit card processing software, please call Neal at xxx-xxx-xxxx"
Response.Write "</br>Error Ref101: client = Server.CreateObject returned 'nothing' "
response.write "</br>(Detailed error: " & Err.Description & ")"
response.write "</br>(Detailed error: " & Err.Number & ")"
Response.End
End If
If client = null Then
Response.write "Error with new credit card processing software, please call Neal at xxx-xxx-xxxx"
Response.Write "</br>Error Ref102: client = Server.CreateObject returned Null "
response.write "</br>(Detailed error: " & Err.Description & ")"
response.write "</br>(Detailed error: " & Err.Number & ")"
Response.End
End If
Also, I'm not sure how the 443 http status gets changed to a 438 Err.Number.
Thanks everyone. I wish I had saved my original error. I've been doing C# so long, I was forgetting how to code VBScript. I tried to add error handling which may have been giving me false results.
If some of the guys who had commented would have put answers, I would have accepted them.
The 443 was another false trail and bad assumption on my part that it was an error, not a port #.
Unfortunately now, I didn't save the original error. I had added code to my original code to give supposedly better or tighter error handling, and adding the "= null" test was a bad idea.
This was a pretty good explanation of VBScript's use of empty vs nothing vs isNull: http://evolt.org/node/346/
I removed that, and the corrected code is:
Err.Clear
On Error Resume Next
'set client = Server.CreateObject("PFProCOMControl.PFProCOMControl.1")
set client = Server.CreateObject("PayPal.Payments.Communication.PayflowNETAPI")
If Err.number > 0 Then
response.write "Error with new credit card processing software, please call Neal at 214-455-8060"
response.write "</br>(Detailed error: " & Err.Description & ")"
response.write "</br>(Detailed error: " & Err.Number & ")"
response.End
End If
If client Is Nothing Then
Response.write "Error with new credit card processing software, please call Neal at 214-455-8060"
Response.Write "</br>Error Ref101: client = Server.CreateObject returned 'nothing' "
response.write "</br>(Detailed error: " & Err.Description & ")"
response.write "</br>(Detailed error: " & Err.Number & ")"
Response.End
End If

VB6 - How to catch exception or error during runtime

I developed an application in VB6. In client's environment it raises runtime errors which I can't reproduce under debugger. Is there any way to get the stacktrace or location of error?
I created log file and
I used
Err.Description,Err.Source
but it gives blank values.
Please help me.
my method(......
On Error GoTo Error_Handler
.........
Error_Handler :
writeToLogFile(Err.Source,Err.Description)
You've probably done something to clear the Err object before writing to the log file. This is very, very easy to do. What you'll want to do is as soon as you detect an error has occurred, grab the error message before doing anything else. Then pass the error message to whatever logging routine you're using. E.g.:
Dim sMsg As String
On Error Goto ErrHandler
' ...code here...
Exit Function
ErrHandler:
sMsg = "Error #" & Err.Number & ": '" & Err.Description & "' from '" & Err.Source & "'"
GoLogTheError sMsg
BTW, thanks for your guys' answers helping me. I'm about half a decade late to the game of VB6. I don't do windows unless forced to. ;)
Anyhow, when doing your error checking, say among 3000 individual record query insertions, I learned a couple tricks. Consider this block of code:
'----- order number 1246-------
On Error Goto EH1246:
sSql="insert into SalesReceiptLine ( CustomerRefListID,TemplateRe..."
oConnection.Execute sSQL
sSql="SELECT TxnID FROM SalesReceiptLine WHERE RefNumber='1246'..."
oRecordset.Open sSQL, oConnection
sTxnId = oRecordset(0)
oRecordset.Close
sSql="INSERT INTO SalesReceiptLine (TxnId,SalesReceiptLineDesc,Sal..."
oConnection.Execute sSQL
EH1246:
IF Err.Number<>0 THEN
sMsg = sMsg & "Order # 1246; sTxnId = " & sTxnId & _
vbCrLf & Err.Number & ": " & Err.Description & vbCrLf
sErrOrders = sErrOrders & "1246,"
End If
On Error GoTo -1
'----- order number 1247-------
On Error Goto EH1247:
When not testing for Err.Number, you'll get a 0: on every order handled. (maybe you don't want that). The On Error GoTo -1 resets the error so that it will work again. Apparently, Err only works "once".
I wrote a php script to build the VB6 source code to run some 8000 odbc queries... :P
Do you definitely, positively have an Exit Function just above the Error_Handler:?
my method(......
On Error GoTo Error_Handler
........
Exit Sub
Error_Handler :
writeToLogFile(Err.Source,Err.Description)
"Exit Sub" should be added before you handle the Error_Handler function.....

Resources