gypsy Posted March 16, 2008 Share Posted March 16, 2008 (edited) Hi, Is this error code handling correct?? See code below. I want to write failures to a file and exit the program at that point. When I look at the documentation for WinWait and WinClose, it says that Success returns a value of '1' and '0' for a Failure. And yet, I run this code and @error appears to have a value of '0' for success. I'm really confused (and a newbie). Thanks! ;Include the following Constants: ;==================== #include <file.au3> #include <GUIConstants.au3> #include <Word.au3> Opt("WinTitleMatchMode", 2) ;Log File ;======== Dim $PublicIP $PublicIP = @IPAddress1 $file = FileOpen("C:\Users\owner\Documents\TEST\file_" & $PublicIP & ".txt", 1) FileWriteLine($file, "Logfile.") FileWriteLine($file, " ") word() Func word() $oWordApp = _WordCreate(@ScriptDir & "\DocTest", 0, 1, 1) If @error <> 0 Then FileWriteLine($file, " Error Code: " & @error) Exit EndIf WinWait("DocTest", "DocTest", 120) If @error <> 0 Then FileWriteLine($file, " Error Code: " & @error) Exit EndIf If WinExists("DocTest") Then If Not WinClose("DocTest") Then EndIf EndIf If @error <> 0 Then FileWriteLine($file, " Error Code: " & @error) Exit EndIf EndFunc Edited March 16, 2008 by gypsy Link to comment Share on other sites More sharing options...
MHz Posted March 17, 2008 Share Posted March 17, 2008 When I look at the documentation for WinWait and WinClose, it says that Success returns a value of '1' and '0' for a Failure. And yet, I run this code and @error appears to have a value of '0' for success.There are two basic error handling concepts in AutoIt not including COM handling. The first is the return value which is a value return from a function. Many functions may return a value of 0 or 1 but other functions are more complicated so they may return other values (even strings) as needed. If WinClose("DocTest") Then ; code here works if the condition was not 0 or not an empty string EndIfoÝ÷ Ù8^±ç(ج¶®º+§+¢ö¥¹æ¬±ë[ɧîËb¢r'P1lج±ë[É'ºè©Ýëh"اn襵û§rبÇhzÈ£ æzØhvìzÛb«®Ì¢Ë_ºw-춱ëmç«®æiÊèˬ¹©eÊØ^më-Á¬¡È^rGèö¢êÞjëh×6_test() If @error Then ; expect to execute this code as @error is set to 1 by the previous function call EndIf Func _test() ; return @error = 1 Return SetError(1, 0, '') EndFunc You have a object to consider removing so you could look at using _WordQuit(). Link to comment Share on other sites More sharing options...
gypsy Posted March 21, 2008 Author Share Posted March 21, 2008 MHz! Thanks very much, this really helped!! Just a follow up question... Is there any (easy) way to get the code to fail? I want to make sure that if there is a failure that is really does get logged. At the moment, I have been putting in a Sleep(10000) which is enough time to do something, (remove window, change name, etc) to get it to fail. THanks for your expertise in this matter! Link to comment Share on other sites More sharing options...
MHz Posted March 22, 2008 Share Posted March 22, 2008 Is there any (easy) way to get the code to fail? I want to make sure that if there is a failure that is really does get logged. At the moment, I have been putting in a Sleep(10000) which is enough time to do something, (remove window, change name, etc) to get it to fail.I am not sure if I can understand that question well enough to give a suitable answer. If I am unsure of a condition, then I would create a test script with enough partial code to test the condition. With adequate error checking in your script, then the script should tell you if something is incorrect with your code, else it may error because of a bad error handling bug and then you may need to debug it. Adding extra lines of code in just for debugging can be regarded as normal if you choose to, and leaving it remain in the source can help if things go wrong later. The choice is yours to make. One of many ways that you could set error for testing. ; Debug settings Global $test[2] ; 2 tests $test[1] = True _some_function() If $test[0] Then SetError(1); debug code only If @error Then FileWriteLine($file, "Test1 logging... " & @error) EndIf _some_other_function() If $test[1] Then SetError(2); debug code only If @error Then FileWriteLine($file, "Test2 logging... " & @error) EndIf ;... ;... You should strive to create good code so you do not need to debug too much hopefully. Link to comment Share on other sites More sharing options...
gypsy Posted March 24, 2008 Author Share Posted March 24, 2008 MHz! Thanks again for the information, this has really helped. I think I've written good code and have been running it for a while, without it failing. I just want to make sure that if/when it should fail, I would be capturing the error. Thanks again! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now