Error Handling Within a UDF

Started by keslaa, November 22, 2016, 06:06:55 AM

Previous topic - Next topic

keslaa

I am using a #DefineFuntion to remove a directory after an uninstall of an application. On about 5% of the machines, Error 1030 is thrown, for various reasons. I want to be able to catch and log that error like I do for the rest of the script. Since this is a #DefineFuntion, how do I go about it? For the rest of the script, I am using IntControl 73 to go to a subroutine which logs the error into a text file before continuing on with the script. Should I add a separate IntControl 73 and corresponding WBERRORHANDLER within the #DefineFuntion? Or should I use a #DefineSubroutine instead as that might be able to utilize the existing IntControl 73?

td

From the IntControl 73 documentation in the Consolidated WIL Help file:

"If you want to have error handling occur with in a User Defined Function / User Defined Subroutine, then IntControl 73 must be defined with in the User Defined Function/User Defined Subroutine."

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

keslaa

Quote from: td on November 22, 2016, 06:36:03 AM
From the IntControl 73 documentation in the Consolidated WIL Help file:

"If you want to have error handling occur with in a User Defined Function / User Defined Subroutine, then IntControl 73 must be defined with in the User Defined Function/User Defined Subroutine."

Thank you. I just saw this as well. Maybe reading everything before I post a question would be better...

keslaa

I am testing this and am now receiving a new error. When trying to close the log file, it is returning the following error:
     3057: Variable could not be converted to a valid number
The line that trips this is

FileClose(LogF)

...where "LogF" is the name of the file. The file opens fine and is written to. It just doesn't like this command.

This is the full code:

#DefineFunction UDFDELTREE(dir,LogF)
IntControl(5,1,0,0,0); Include hidden files
IntControl(73, 2, 0, 0, 0)
section = "UDFDELTREE"
DirChange(dir); change to target directory
topdir=DirGet(); Delete all files in target
If FileItemize("*.*") <> ""
FileAttrSet("*.*","rash")
FileDelete("*.*")
endif
dirlist=DirItemize("*.*"); Get list of subdirectories
dircount=ItemCount(dirlist,@TAB);Process list
For xx=1 To dircount
thisdir=ItemExtract(xx,dirlist,@TAB)
UDFDELTREE(thisdir)
Next
DirChange(".."); bump up one directory level
DirAttrSet(dir,"rash"); remove that directory
DirRemove(dir)

;======================================================
:WBERRORHANDLER
error = LastError()
FunctionHandle = FileOpen(LogF,"APPEND")
text = "A bunch of lines of text regarding the error"
FileWrite(FunctionHandle, text)
FileClose(LogF)
IntControl(73, 2, 0, 0, 0)
Return; WBERRORHANDLER
;======================================================
#EndFunction

td

No sense in rewriting what has already been written so from the Consolidated WIL Help file:

"FileClose
Closes a file.

Syntax:
FileClose (filehandle)

Parameters:
(i) filehandle same integer that was returned by FileOpen."
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

keslaa

Should be FileClose(FunctionHandle).

Wow. What an idiot. I hate it when I don't see the obvious. Sorry to waste your time.