Jump to content
Sign in to follow this  
square65

@error for Try/Catch logic

Recommended Posts

square65

This thread shows a way to use @error to simulate a try/catch block.

What I don't understand is how this can possibly work.

An error terminates execution already. How is @error ever going to be accessible after an error anyway?

Share this post


Link to post
Share on other sites
BrewManNH

@error is only a macro that is set to the error returned from a function call, it doesn't do anything by itself, and it certainly doesn't cause the script to stop.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
square65

Can you give me an example of @error in practical use?

I never said anything about @error causing any script to stop.

Share this post


Link to post
Share on other sites
BrewManNH

I must have misread what you said here.

An error terminates execution already. How is @error ever going to be accessible after an error anyway?I

 Regardless, depending upon the error type, most aren't fatal. If you need an example of how @error works, please look at the help file and look at the majority of the example scripts in it, I'm sure you'll find @error used in plenty of places.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
square65

I did look at the documentation.

That led me here to ask what the point of @error was.

In a Try/Catch block you can do

Try{code1} Catch{code2} and if code1 literally breaks the program and throws an exception, then Catch can catch it. The program can continue.

In AutoIt if some program breaking error occurs, it simply ends the script.

@error is just equal to the return value of a function. If an error occurs, it still ends the script.

Share this post


Link to post
Share on other sites
square65

Btw I couldn't find any documentation about @error itself. Just SetError to manually set it. I want to know exactly how it works. Not how to do one specific thing with it.

Share this post


Link to post
Share on other sites
iamtheky

In AutoIt if some program breaking error occurs, it simply ends the script.

Do you have a reproducer?  Seems this would be easier discussed in code.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
BrewManNH

Refer back to my last post, in that I told you to look at the example scripts in the help file. You will find dozens of function examples that use @error in the scripts for those functions. For example, you can look at the help file example for the StdErrRead command to see how it is used in that script, or any of the _Excel_* example scripts.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jguinch

Example for RegRead (see helpfile for @error description for each function)

$sVal = RegRead ( "HKLM\Software\Microsoft", "SomeValue" )
If @error <> 0 Then 
    Switch @error
        Case 1
            $sError = "unable to open requested key"
        Case 2
            $sError = "unable to open requested main key"
        Case 3
            $sError = "unable to remote connect to the registry"
        Case -1
            $sError = "unable to open requested value"
        Case -2
            $sError = "value type not supported"
    EndSwitch
    MsgBox(16, "RegRead error", "An error occured : " & $sError)
EndIf

 

Share this post


Link to post
Share on other sites
square65

Example for RegRead (see helpfile for @error description for each function)

$sVal = RegRead ( "HKLM\Software\Microsoft", "SomeValue" )
If @error <> 0 Then 
    Switch @error
        Case 1
            $sError = "unable to open requested key"
        Case 2
            $sError = "unable to open requested main key"
        Case 3
            $sError = "unable to remote connect to the registry"
        Case -1
            $sError = "unable to open requested value"
        Case -2
            $sError = "value type not supported"
    EndSwitch
    MsgBox(16, "RegRead error", "An error occured : " & $sError)
EndIf

 

​Thanks that explained it perfectly.

Share this post


Link to post
Share on other sites
iamtheky

Btw I couldn't find any documentation about @error itself. Just SetError to manually set it. I want to know exactly how it works. Not how to do one specific thing with it.

​I dont understand how a case statement to match numbers helps you gain more of an understanding of @error, moreso than say building your own example with seterror.

$sInput = inputbox("Test Error Levels" , "Type a six character string of only letters" , "ABCDEF")
$aTest = StringTest($sInput)
If @Error = 0 then
    msgbox(0, '' , $aTest[0])
Else
    msgbox(0, '' , $aTest[0] & @CRLF & "Error: " & @Error & @CRLF & "Extended: " & @Extended & @CRLF & "Text: " & $aTest[1])
Endif


Func StringTest($sString)

local $aOut[2]

    If StringLen($sString) = 6 AND StringIsAlpha($sString) Then
        $aOut[0] = "GOOD STRING"
    Else
        $aOut[0] = "BAD STRING"

        If StringLen($sString) < 6 AND StringIsAlpha($sString) then
            $aOut[1] = SetError(1 , 6 , "The string is too short")
        ElseIf StringLen($sString)  > 6 AND StringIsAlpha($sString) then
            $aOut[1] = SetError(2 , 7 , "The string is too long")
        ElseIf  StringLen($sString)  = 6 AND StringIsAlpha($sString) = 0 then
            $aOut[1] = SetError(3 , 8 , "The string does not contain all letters")
        ElseIf  StringLen($sString) <> 6 AND StringIsAlpha($sString) = 0 then
            $aOut[1] = SetError(4 , 9 , "The string is all jacked up")
        EndIf

    EndIf


return $aOut

EndFunc

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
square65

Well it's like this. The guy two posts above you explained it so I understood it. Understood it perfectly, including what you said.

So I thanked him right?

It gets crazier though.

Shortly after, you come in with your post which was completely unnecessary and even had kind of a rude tone to it.

This world we live in! hahahaha

Edited by square65

Share this post


Link to post
Share on other sites
iamtheky

Not rude i promise, I just dont see the try/catch answer anywhere in there at all.  That is awesome that you came about the answer that way, but as with every thread, the resulting answer and discussion is not always there for the OP.  I find the best answers come a few posts after mine, because the solution is not optimal (but it is confident in its mediocrity).   And my forum tone is generally perceived as provocative, when in reality its just poorly written and lacking of niceties.

I just really dont understand, because imho, try/catch would be what you are doing to set the error, so that it can then be checked in the fashion above, all without the script exploding.  

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
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
Sign in to follow this  

×