Jump to content

How to end script no matter what the error?


sloan
 Share

Recommended Posts

I'm missing something here. I've searched the forums and Google and haven't come up with anything. I'm probably using the wrong search terms.

Anywho, I have been using AutoIT for the last week and am pretty proud of myself. I've created a script that will configure WPA2 for a wireless card in XP. It works. The only thing I'm missing is that when the script "pauses" or errors out, no matter where it is at in the script, I want it to pop up an message box saying so. After the OK button is pressed, the script ends.

I've tried If statements and While loops and have come up short. For example:

if @error = 1 then

msgbox(0+48, "Error encountered. Exiting.", "An error has occured. ")

Exit

Putting the Endif below Exit doesn't work nor does putting it at the end of the script yield the desirable results.

Like I said, I know I'm missing something. This is the finishing touch on this script and with it I stand to impress a few bosses as they have no expectation of this even being possible. I apologize in advance if this is covered elsewhere and am deeply greatful for any help provided.

I haven't provided my code as it sounds as if this may be a universal solution (i.e., the solution can pretty much work for any script that needs to exit if an error is encountered).

Thanks again,

Sloan

Link to comment
Share on other sites

@error only works where functions etc actually set error. If your using COM code then you need to use the ObjEvent function to trap COM errors. Then there is the RunErrorsFatal option to set silent to @error and use your own code to handle it. Then others return values to indicate error so you need to handle then by checking return value. Some (if any at all) may even use @extended in a bid to notify of error, so you need to address them.

So there is no one size fit all for addressing errors. Most functionality follow a consistant concept. Without knowing your code, then it is hard to give a definitive answer to your error handling.

Edited by MHz
Link to comment
Share on other sites

So there is no one size fit all for addressing errors. Most functionality follow a consistant concept. Without knowing your code, then it is hard to give a definitive answer to your error handling.

Thank you for your reply. Like I said, I didn't post my code because I figured maybe I could put something at the beginning and end of the code and tell it, "Hey, if anything happens in between here, let the user know and then exit." Looks like I may have been wrong. :whistle:

I'm open for other suggestions (if any).

Sloan

Link to comment
Share on other sites

You must check for an error after each code segment that might produce an error.

$re=Run("foo.exe")
If @error=1 Then
MsgBox(0, "Foo Error", "An error occured running foo.exe. Exiting...")
Exit
EndIf

Or make a function;

$re=Run("foo.exe")
If @error=1 Then _err(0, "Foo Error", "An error occured running foo.exe. Exiting...")
$re=Run("foo2.exe")
If @error=1 Then _err(0, "Foo2 Error", "An error occured running foo2.exe. Exiting...")

Func _err($type, $caption, $msg)
MsgBox($type, $caption, $msg)
Exit
EndFunc

@error only works for return functions and does not check for errors in your script unless you set the @error variable yourself. Autoit handles that unless you set RunErrorsFatal false or set a function for ObjEvent errors for objects.

Link to comment
Share on other sites

You must check for an error after each code segment that might produce an error.

$re=Run("foo.exe")
If @error=1 Then
MsgBox(0, "Foo Error", "An error occured running foo.exe. Exiting...")
Exit
EndIf

Or make a function;

$re=Run("foo.exe")
If @error=1 Then _err(0, "Foo Error", "An error occured running foo.exe. Exiting...")
$re=Run("foo2.exe")
If @error=1 Then _err(0, "Foo2 Error", "An error occured running foo2.exe. Exiting...")

Func _err($type, $caption, $msg)
MsgBox($type, $caption, $msg)
Exit
EndFunc

@error only works for return functions and does not check for errors in your script unless you set the @error variable yourself. Autoit handles that unless you set RunErrorsFatal false or set a function for ObjEvent errors for objects.

Thank you for your reply. I've tried something similar to both your examples with no luck. Thank you again!

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