Jump to content

If @error


Recommended Posts

Hello, 

i have a quick and simple question. 

 

Is someting like this possible

 

If @error = 00 Then

MsgBox(0,"test", "00 : " )

ElseIf @error = 230 Then
   MsgBox(0 ,"test","230 : ")


  EndIf

 

Because i need to use the errorcode for moving on in my script.

 

Thank you !

Link to comment
Share on other sites

For errorcode 00 it is working. But for errorcode 230 not.

 

It only shows me the msg box for case 0 and not 230 

 

FileChangeDir("C:\Program Files\SEH Computertechnik GmbH\SEH UTN Manager") 
RunWait(@ComSpec & " /c " & "utnm.exe") 
Global $test = RunWait(@ComSpec & " /c " & 'utnm /c "activate xxxxxx 11" /k "BAUMANAGER"')
MsgBox(0,"test", "Error : " & $test & @error)


Switch @error
   Case 230
      MsgBox(0,"Funkt", "23 : ")
   Case 00
      MsgBox(0,"Funkt", "00 : ")
EndSwitch
 

Link to comment
Share on other sites

@error is set by the last executed function. In you case, @error is set to 0 by MsgBox. Remove the MsgBox line after the RunWait function :

 

RunWait(@ComSpec & " /c " & "utnm.exe") 
Global $test = RunWait(@ComSpec & " /c " & 'utnm /c "activate xxxxxx 11" /k "BAUMANAGER"')
; MsgBox(0,"test", "Error : " & $test & @error)

Switch @error
   Case 230
      MsgBox(0,"Funkt", "23 : ")
   Case 0
      MsgBox(0,"Funkt", "00 : ")
EndSwitch

edit : in the same time with pixelsearch 😀

Edited by jguinch
Link to comment
Share on other sites

5 minutes ago, pixelsearch said:

In all scripts, the best way to avoid all this headache is to transfer @error in a variable, immediately after the line you want to test if it succeeded or not, for example :

 

4 minutes ago, jguinch said:

@error is set by the last executed function.

@Marcel2304 : These tips are really important. Even if a function like e.g. ConsoleWrite does not provide an error code (according to the help), @error will be reseted.

Example :

; 1 :
FileRead(@ScriptDir & "\blabla.txt") ; <-- a file, that does not exist
ConsoleWrite("Error = " & @error & @CRLF & @CRLF) ; <-- Error = 1


; 2 :
FileRead(@ScriptDir & "\blabla.txt") ; <-- a file, that does not exist
ConsoleWrite("Output :" & @CRLF)
ConsoleWrite("Error = " & @error & @CRLF & @CRLF) ; <-- Error = 0

; 3 : save @error in a var (mentioned by @pixelsearch):
Local $iError
FileRead(@ScriptDir & "\blabla.txt") ; <-- a file, that does not exist
$iError = @error
ConsoleWrite("Output :" & @CRLF)
ConsoleWrite("Error = " & $iError & @CRLF) ; <-- Error = 1

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Because you (probably) got a line that resets @error to 0, after your last RunWait and before the Switch line. Could you please tell us what happens if you script it like this and what is written in the Console Window ?

FileChangeDir("C:\Program Files\SEH Computertechnik GmbH\SEH UTN Manager") 
RunWait(@ComSpec & " /c " & "utnm.exe") 
Global $test = RunWait(@ComSpec & " /c " & 'utnm /c "activate xxxxxx 11" /k "BAUMANAGER"')

Local $iKeep_error = @error ; to be placed here, immediately after RunWait() line
ConsoleWrite("$iKeep_error = " & $iKeep_error & @CRLF)

MsgBox(0, "Result", "Test = " & $test & "   Error = " & $iKeep_error)

Switch $iKeep_error
   Case 230
      MsgBox(0,"Funkt", "230 : ")
   Case 0
      MsgBox(0,"Funkt", "0 : ")
   Case Else
      MsgBox(0,"Funkt", $iKeep_error)
EndSwitch

 

Edited by pixelsearch
Added a Case Else, in case of...
Link to comment
Share on other sites

Something else read in the help file, topic RunWait :

Return Value
Success: the exit code of the program that was run. 
Failure: sets the @error flag to non-zero.

So what is this 230 you mention ?

If 230 is the correct return value you're expecting, then it has nothing to do with @error, which will be 0 if RunWait succeeded (returning 230)

Link to comment
Share on other sites

29 minutes ago, Marcel2304 said:

... but what i don't unterstand is why only case 0 is triggerd even when its case 230. 

Probably because the error code 230 gets reseted to 0, as @pixelsearch already suspected.

Test :

_SimulateError()
Local $iError = @error
Switch $iError
    Case 00
        ConsoleWrite("Error = " & $iError & @CRLF)
    Case 230
        ConsoleWrite("Error = " & $iError & @CRLF)
    Case Else
        ConsoleWrite("something else" & @CRLF)
EndSwitch

Func _SimulateError()
    SetError(230) ; <-- set a value for @error, e.g. 00, 230 etc.
EndFunc   ;==>_SimulateError

EDIT : I see, that @pixelsearch just pointed out the difference between @error and return value :lol:.

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...