Jump to content

@error Vs If Blah <> 1 Then...


Recommended Posts

Can someone explain to me the benefit of using @error over if blah <> 1 then msgbox "blahblahblha"?

I don't quite understand and am in need of putting some error handling into my script.

Thanks

Neoborn :)

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

Not sure exactly what you are asking, but most functions have returned codes of various error levels. The error level is not necessarily an error; it really depends upon what is meant. You can find out what a functions error code is by examining the help file. In other words @error is only as useful as the function it follows designed it to be.

I know you think that I know I understand what you said, but I am not sure that what I understood is what you thought.

Link to comment
Share on other sites

if you make :

$move = FileMove("C:\" , "D:\" )
if @error = 1 then
exit
endif

an error occured, the function set the error to 1

SetError(1)

if you make :

$move = FileMove("C:\" , "D:\" )
if $move <> 1 then
exit
endif

the return value of $move equals not 1 that means an error happend that looks like this :

SetError(1) ; @error = 1

Return 0 ; $move = 0

hope ya understand

My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity
Link to comment
Share on other sites

Do they not essentially say the same thing?

I have

Func CreateDir()
   DirCreate("C:\temp")
   If DirCreate("C:\temp") <> 1 Then
      MsgBox(4096, "Error", "Failed To Create Directory ""temp""...", 15)
   EndIf
EndFunc

Is that not the same as:

DirCreate("C:\temp")
   If @error = 1 Then
      MsgBox(4096, "Error", "Failed To Create Directory ""temp""...", 15)
   EndIf

Also does the exit just exit out of that func?

Thanks guys :)

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

Func CreateDir()

If DirCreate("C:\temp") <> 1 Then

MsgBox(4096, "Error", "Failed To Create Directory ""temp""...", 15)

EndIf

EndFunc

1. this is enough :)

2. sometimes @error = 4027 or some other number, check TCP @error's

My UDF's : Startet on : 06.06.2006_CaseSearchOrReplaceStr();~> Searches OR Replaces a String,;~> With or Without Casesensivity
Link to comment
Share on other sites

lol :) can you answer my questions?

Do they not essentially say the same thing?

I have

Func CreateDir()
   DirCreate("C:\temp")
   If DirCreate("C:\temp") <> 1 Then
      MsgBox(4096, "Error", "Failed To Create Directory ""temp""...", 15)
   EndIf
EndFunc

Is that not the same as:

DirCreate("C:\temp")
   If @error = 1 Then
      MsgBox(4096, "Error", "Failed To Create Directory ""temp""...", 15)
   EndIf

Also does the exit just exit out of that func?

Thanks guys :(

Edited by Neoborn

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

Also how do you make it so the next function doesn't run until the last one has completed?

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

1

Do they not essentially say the same thing?

no... because in the first example you have

DirCreate("C:\temp")
   If DirCreate("C:\temp") <> 1 Then

it should be

If DirCreate("C:\temp") <> 1 Then

then they are very close to the same

2

Is that not the same as:

yes... after the fix i stated above... also this is a "preferred" way

3

Also does the exit just exit out of that func?

"Exit"... ALWAYS exits/ends the script/program...

to leave a function, use "Return"

4

Also how do you make it so the next function doesn't run until the last one has completed?

the next function should not execute, until the current function is completed, by way of finishing, returning early, or other intervention

8)

NEWHeader1.png

Link to comment
Share on other sites

1. So @error is the better way / standard / coding practices?

2. What is the best way to go through a bunch of functions then I currently have them

function1()

function2()

function3()

and I am presuming by this they will run one after the other sequentially, is this correct?

Waht if function2 takes longer, does that matter because function3 won't run until function2 is complete?

Thanks Val.

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

kewlio, you rox dood :) ty much. And btw thank you for auto123 it rox as well. Not finished it yet though :( <3

Would you put error handling all the way through? on every func?

Edited by Neoborn

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

1. So @error is the better way / standard / coding practices?

Not preferred.

Using Booleans is far more easier and efficient then using @error. @error should be used when a Boolean is unsuitable for a functions return value.

If DirCreate("C:\temp") Then

Using Booleans also allow easier operator usage.

Any UDFs that I create, use @error as a last resort.

:)

Link to comment
Share on other sites

Not preferred.

Using Booleans is far more easier and efficient then using @error. @error should be used when a Boolean is unsuitable for a functions return value.

If DirCreate("C:\temp") Then

Using Booleans also allow easier operator usage.

Any UDFs that I create, use @error as a last resort.

:)

You're confusing terminology. The @error macro can be used as a boolean, too, and in fact it should be unless there is a specific need to test the value of @error. Using something as a boolean simply means you rely on the language to interpret the value to either true or false rather than explicitly comparing it to another value. The below fragment is a boolean just like your example:

If 1 Then

To the OP, return values are used to signal a functions success or failure. In the event there is more than one way to fail but only one valid return value for failure, then @error can be used to store a separate value useful for identifying the specific cause of failure. Lastly, there may not be a return value suitable to use for a failure value so in that case @error is used as the sole mechanism for signaling an error. Whichever method the function uses will be documented. There is no "this way is better than that way" for detecting errors with functions because not all functions can use the same method. You read the documentation to see how the function will behave and then you write code to handle what it says it might do. Don't fall into the trap that others do and assume every function will set @error on failure and try to write lazy code of checking only @error. Do what's relevant for the function at hand and nothing more or less.

Link to comment
Share on other sites

Is this the best way then?

DirCreate("C:\temp")
   If @error = 1 Then
      MsgBox(4096, "Error", "Failed To Create Directory ""temp""...", 15)
   EndIf

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

Is this the best way then?

DirCreate("C:\temp")
   If @error = 1 Then
      MsgBox(4096, "Error", "Failed To Create Directory ""temp""...", 15)
   EndIf
Did you even read my post?
Link to comment
Share on other sites

I did skim it but I felt out of place intervening in your geek speak and the symantics of the programming language with the other fellow :). If you could put it into simple man words I could probably assimilate it.

The @error macro can be used as a boolean, too, and in fact it should be unless there is a specific need to test the value of @error

.

= Always use @error unless you need to test the value of @error...yes?

There is no "this way is better than that way" for detecting errors with functions because not all functions can use the same method.

= Ahh ok kewl so if the documentation says "Returns 1 if success and 0 if fail then I can just use the @error fine

You read the documentation to see how the function will behave and then you write code to handle what it says it might do.

= Can you give me an example from the help file where you would use @error and where you would not?

Don't fall into the trap that others do and assume every function will set @error on failure and try to write lazy code of checking only @error. Do what's relevant for the function at hand and nothing more or less.

= OK, in the cases above with my dircreate example what would you use?

Edited by Neoborn

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Link to comment
Share on other sites

I did skim it but I felt out of place intervening in your geek speak and the symantics of the programming language with the other fellow :) . If you could put it into simple man words I could probably assimilate it.

Is English not your primary language? That was English, not "geek speak". It doesn't come much simpler than how I put it.

= Always use @error unless you need to test the value of @error...yes?

Qualify this statement. I'm still under the impression that you are looking for an answer along the lines of "always do this in this scenario" and that is simply not the answer. That answer does not exist. Every function has to be viewed seperate from every other function to determine the best method for handling it's failures.

= Ahh ok kewl so if the documentation says "Returns 1 if success and 0 if fail then I can just use the @error fine

What on earth makes you say such a ridiculous thing? Did you even read my post above?

= Can you give me an example from the help file where you would use @error and where you would not?

I would use @error on functions where I couldn't detect the failure condition in any other way. I wouldn't use it on functions that I could unless I had a need for the @error value specifically.

= OK, in the cases above with my dircreate example what would you use?

I would use the return value exactly like several thousand people have demonstrated above.
Link to comment
Share on other sites

Thank you Valik, I still don't quite understand but you're just too smart and I'm too stupid to understand you. I'll ask th.meger over messenger, it'll be easier that way.

Thank you for your effort and if other people understand then you gone and done good! :)

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

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...