#181384 - 2007-10-09 04:46 PM
Is there an easy way to set the @error macro to zero?
|
RomanB
Fresh Scripter
Registered: 2006-10-20
Posts: 15
Loc: Bonn, Germany
|
Hi guys, though working with kix quite a while and searching the forum(s) intensively I didn't find an elegant way to reset the value of @error to zero. I always use a dummy command such as "cd .", which is always successful so that afterwards @error is 0. Does anybody know a better way? Thnx!
if @error<>0
? $errormessage
cd "." ;sets @error to 0
else
? "Success"
endif
|
|
Top
|
|
|
|
#181386 - 2007-10-09 05:18 PM
Re: Is there an easy way to set the @error macro to zero?
[Re: RomanB]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
The easy way to set @ERROR to any value is:
$=Execute("Exit ErrorValueHere")
Here is a very useful script I keep to hand to search for error codes that I might want to use in my own functions:
BREAK ON
"Enter a string to search the error messages for:"
Gets $a
for $i = 0 to 9999
$=Execute("Exit "+$i)
If InStr(@SERROR,$a) @ERROR " " @SERROR ? Endif
next
|
|
Top
|
|
|
|
#181387 - 2007-10-09 05:20 PM
Re: Is there an easy way to set the @error macro to zero?
[Re: RomanB]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
|
Can't recall a situation where I needed the value reset, since most functions will set it, and I'll only test it where I need to know the last status. I do, however, save the error status, or at least an error flag if errors are non-fatal, something like:
; Set errflag to last error, but don't change previous value if ; the current error value is zero $ErrFlag = IIF(@ERROR, @ERROR, $ErrFlag)
I suppose if I HAD to reset the macro, something like what you proposed would do it.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#181391 - 2007-10-09 05:42 PM
Re: Is there an easy way to set the @error macro to zero?
[Re: Richard H.]
|
RomanB
Fresh Scripter
Registered: 2006-10-20
Posts: 15
Loc: Bonn, Germany
|
Hi Richard and Glenn! Thanks! At first a question: doesn't exit the script? Yes, I checked it out. So why doesn't the script exit???  Maybe I should illustrate the situation, in which I need this handling. I call a function which needs to check @error for its return value. This function is called within a while loop:
;-------------------
function pingable
shell "cmd /c ping " + $machine + " >NUL"
if @error=0
$pingable='yes'
else
$pingable='no'
endif
cd "." ;reset @error regardless of value od $pingable
endfunction
;-------------------
...
while @error=0
...
$rc = pingable($machine)
;something like next machine
...
loop
As I presumed that @error is kind of global I thought I have to reset it to zero when leaving the function. Regarding your answers must that be wrong? Regards, Roman
Edited by RomanB (2007-10-09 05:49 PM) Edit Reason: wrong line in post, sorry
|
|
Top
|
|
|
|
#181394 - 2007-10-09 06:03 PM
Re: Is there an easy way to set the @error macro to zero?
[Re: RomanB]
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
It is somewhere in the FAQ http://www.kixtart.org/forums/ubbthreads...true#Post130059
BREAK ON Dir("C:\NonExistingFile.Extension") "Error = " @ERROR ? "Reset Error" ? ResetError() "Error = " @ERROR ? Get $SO
Function ResetError() Exit 0 EndFunction |
|
|
Top
|
|
|
|
#181396 - 2007-10-09 06:56 PM
Re: Is there an easy way to set the @error macro to zero?
[Re: RomanB]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
|
When you use the Exit command in kix - either from a function or the main script, you pass the argument back to the calling environment, so
C:>kix32 test.kix (command prompt, calls Kix32.exe with arg "test.kix")
$ = test('me!') (Kix32 runs script - Script calls Test() UDF)
If $ID = 'me!' Exit 5 EndIf (Test() decides it doesn't like you, so does Exit 5 (access denied) and thus sets the @ERROR macro.)
Logic in script checks Error macro and exits with same error (back to next line after Test() call..) If @ERROR @SERROR ? Exit @ERROR EndIf
Back at the command prompt, the exit status of the script has been passed to the command interpreter, which can be viewed in the %ERRORLEVEL% variable. Echo %ERRORLEVEL%
You don't need to CLEAR the error code, simply add the appropriate EXIT @ERROR line, so the error value from within the function is returned.
This works for simple functions like yours, but logic can change as they get more complex. Consider:
Function Alive($Host)
Shell '%COMSPEC% /c Ping ' + $Host + '>NUL:'
If @ERROR
$Alive = 0 ; return 0, not alive
Exit @ERROR
Else
$Alive = 1 ; return 1, is alive
Exit @ERROR ;
EndIf
EndFunction
With this kind of logic, you can do things like
If Alive('some_computer')
'Some_Computer is alive!' ?
Else
'Error ' @ERROR ' occured while pinging Some_Computer!' @CRLF '(' @SERROR ')' ?
EndIF
Give it a try...
Glenn
PS - Note that this example sorta works, and is OK for explaining the logic, but is not a valid or reliable method of ping checks!
Edited by Glenn Barnas (2007-10-12 01:19 PM)
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#181460 - 2007-10-10 09:43 PM
Re: Is there an easy way to set the @error macro to zero?
[Re: Glenn Barnas]
|
RomanB
Fresh Scripter
Registered: 2006-10-20
Posts: 15
Loc: Bonn, Germany
|
Many thanks in alphabetical order to Glenn, Richard an Witto.  Any of your approaches has its advantages depending on the situation. I really appreciate your efforts and learned a lot. My problem is the lack of colleagues doing kix, which might influence my scripting due to resulting lack of exchange of ideas. I'll try to use the forums more frequently to reduce my handicap. Regards, Roman
|
|
Top
|
|
|
|
#181557 - 2007-10-12 01:24 PM
Re: Is there an easy way to set the @error macro to zero?
[Re: Witto]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
|
|
|
Top
|
|
|
|
#181575 - 2007-10-12 11:46 PM
Re: Is there an easy way to set the @error macro to zero?
[Re: Glenn Barnas]
|
RomanB
Fresh Scripter
Registered: 2006-10-20
Posts: 15
Loc: Bonn, Germany
|
*lots of fun* " ... nobody expects the spanish [scripting] inquisition. Our chief weapon is surprise, surprise and fear ... ... and ruthless efficiency ..."
Apart from the fact that I didn't even dream of rising SUCH a discussion: should mention that Jochen forgot the "-n 1" parameter, which is very gut regarding golfing aspects, but performance ... just kidding!
And Witto, what means p.e.? "Par exemple"? Please don't mind, je ne me moque pas de toi. Roman 
Edited by RomanB (2007-10-13 12:23 AM) Edit Reason: thinkin
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 778 anonymous users online.
|
|
|