Page 1 of 2 12>
Topic Options
#181384 - 2007-10-09 04:46 PM Is there an easy way to set the @error macro to zero?
RomanB Offline
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!
 Code:
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 Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
The easy way to set @ERROR to any value is:
 Code:
$=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:
 Code:
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 Administrator Offline
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! \:D

Top
#181389 - 2007-10-09 05:25 PM Re: Is there an easy way to set the @error macro to zero? [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Normally you won't ever need to reset @ERROR, but I used to get problems with some COM stuff which would set the error if there was a problem, but wouldn't reset it when the action succeeded. So if a failed action was followed by a successful one @ERROR would still be set.

It's pretty rare, and I haven't come across it in quite a while.

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 Offline
Fresh Scripter

Registered: 2006-10-20
Posts: 15
Loc: Bonn, Germany
Hi Richard and Glenn!
Thanks!
At first a question: doesn't
 Code:
$RC=Execute("Exit 0")
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:
 Code:
;-------------------
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 Offline
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 Administrator Offline
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:
 Code:
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
 Code:
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! \:D

Top
#181415 - 2007-10-10 09:23 AM Re: Is there an easy way to set the @error macro to zero? [Re: Glenn Barnas]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Quote:
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?


Not totally wrong.

@ERROR is a macro, but you can consider it to be a special type of global variable.

When you use a KiXtart function, command, user defined function or some COM entities the value of @ERROR is *automatically* set to the exit / error value.

If you don't explicitly set an exit value then the value of @ERROR is undefined so the simple rule is *always* set an exit value when leaving a UDF even if it doesn't seem to be relevant, and you won't have any problems.

The exit value of a function is set by using "Exit n" where "n" is an integer. Exit only exits the execution sandbox that it is *currently* in - function, script or Executed() code - and returns to the calling element. If you want to shut down the entire KiXtart instance you would use Quit.

BTW, you can get even trickier with Glenn's code:
 Code:
Function Alive($Host)
 Shell '%COMSPEC% /c Ping ' + $Host + '>NUL:'
 $Alive=Not @ERROR
 Exit @ERROR 
EndFunction


Do you see what I did there? ;\)

Top
#181419 - 2007-10-10 01:01 PM Re: Is there an easy way to set the @error macro to zero? [Re: Richard H.]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Without giving away Richard's sweet golfing trick, the point we're trying to make is that functions usually return TWO pieces of information - a Return Value and an Exit (or Status) Code.

The Exit Code is zero if success, or the error code if there was an error. This is so you can tell what kind of error occurred and possibly take corrective action.

The return value is the data returned by the function - the answer to the question, so to speak. That's what my function was returning - 1=alive and 0=dead. I could have returned yes/no or alive/dead values, but 1/0 are easier to process by the software. In fact, the values Zero / Not Zero are more accurate, but 1/0 are easier examples.

Richard's change to my example is often used to simplify this yes/no type of logic, but won't work if you need a "real" answer, like a calculation result or name of a computer.

Glenn

PS - "golfing" is a term we use to reference shortening of code, thus reducing the number of (key) strokes. ;\)


Edited by Glenn Barnas (2007-10-10 01:20 PM)
Edit Reason: Added PS
_________________________
Actually I am a Rocket Scientist! \:D

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 Offline
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
#181469 - 2007-10-10 11:14 PM Re: Is there an easy way to set the @error macro to zero? [Re: RomanB]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Groan! Already talking in Golf terms! ;\)

Trust me - you'll get lots of idea exchange here.

G-
_________________________
Actually I am a Rocket Scientist! \:D

Top
#181546 - 2007-10-12 09:03 AM Re: Is there an easy way to set the @error macro to zero? [Re: Glenn Barnas]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
 Originally Posted By: Glenn Barnas
 Code:
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



PS - Note that this example sorta works, and is OK for explaining the logic, but is not the most reliable method of ping checks!


Whoa Glenn, you should mention this more strongly! It is not just not the most reliable method, it's simply NO Method !
It will happily report 'Alive' state no matter what is fact on the other end of the line until you delete ping.exe or something
_________________________



Top
#181547 - 2007-10-12 09:12 AM Re: Is there an easy way to set the @error macro to zero? [Re: Jochen]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
I seem bound to repeat that again and again

* The only way to use ping is this:
 Code:
Function Alive($Host)
 shell '%COMSPEC% /c Ping ' + $Host + ' |find /c "TTL=" >nul 2>nul'
 If @ERROR
  $Alive = 0 ; return 0, not alive
  Exit @ERROR
 Else
  $Alive = 1 ; return 1, is alive
  Exit @ERROR ; 
 EndIf
EndFunction
_________________________



Top
#181548 - 2007-10-12 09:21 AM Re: Is there an easy way to set the @error macro to zero? [Re: Jochen]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
 Quote:
I seem bound to repeat that again and again

Hey Guys, Jochen still hasn't realised that when we post PING code we strip out the TTL check, just to wind him up. Teehee. \:\/

Top
#181550 - 2007-10-12 10:15 AM Re: Is there an easy way to set the @error macro to zero? [Re: Jochen]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Maybe you should write an UDF for this?
Top
#181554 - 2007-10-12 10:59 AM Re: Is there an easy way to set the @error macro to zero? [Re: Witto]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
He did... it's just so old that it falls outside of the search paramaters...

Ping() - http://www.kixtart.org/forums/ubbthreads...=true#Post82068

Top
#181555 - 2007-10-12 11:58 AM Re: Is there an easy way to set the @error macro to zero? [Re: Allen]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
It is slightly different. It writes p.e. a temporary file to a directory that is maybe not accessible for the user???
The above function works without any temp file. Much nicer.

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 Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
How did this go from discussing the proper way of returning from functions to discussing how bad our simple ping example was? ;\)

Mebbe I should go back and highlight it in 14pt, bold, red text? (oh, wait.. I did!) \:D

What if I attach virtual cattle prods preventing anyone else from actually using the "shockingly bad" code? \:o (dang - where's that "fried" emoticon when you need it?)

\:\) Glenn
_________________________
Actually I am a Rocket Scientist! \:D

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 Offline
Fresh Scripter

Registered: 2006-10-20
Posts: 15
Loc: Bonn, Germany
\:D *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
#181601 - 2007-10-14 07:21 PM Re: Is there an easy way to set the @error macro to zero? [Re: Witto]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
 Originally Posted By: Witto
It is slightly different. It writes p.e. a temporary file to a directory that is maybe not accessible for the user???
The above function works without any temp file. Much nicer.


It writes to a temporary file only if the caller wishes to retrieve the IP-Address from a given name, if the focus is just on returning failure or success it doesn't do it ... gawd, nobody really reads my code

;\)
_________________________



Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 978 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.074 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org