Chimaera

Correct error checking procedure

17 posts in this topic

#1 ·  Posted (edited)

Im just starting to go through and try and add as much error checking to my scripts to help with pinning down problems

Not that i have many problems it just helps

Global $netsh1 = ShellExecuteWait("netsh.exe", 'advfirewall firewall set rule group="Network Discovery" new enable=Yes', "", "", @SW_HIDE)

For eg this above i need to check the Shellexecute Wait for correct return value and my question is this

In the helpfile it is like this

Success: the exit code of the program that was run.

Failure: sets the @error flag to non-zero. 

 So do i check for

If $netsh1 < 0 Then

Or  

If @error then
;stuff done here

or

If $netsh1 = @error Then
;stuff done here

I generally add a variable so which is the right way to do it?

generally speaking i will be writing to a logfile so i can check for stuff later

Edited by Chimaera

Share this post


Link to post
Share on other sites



The help file says that on error macro @error is set to non zero.

As you want to check for an error I would do:

If @error Then ... ; Do your error handling here (Exit script, end function with an error code ...)

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

This is what i was getting at

should error be specific like

If $netsh1 < 0 Then

Or like your example

are we supposed to worry about the specific value of the exit code or is that just for more detailed scripts

I have some bigger ones that deal with robocopy for eg that yes has 12 exit codes so it does matter but if im error checking small stuff which is the correct way to do it?

Will it increase the load if i add error at every turn so that the script has more stuff to do?

Edited by Chimaera

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You should check for an error, the return code from the program being run may be anything, the error code is specific. Anything other than 0 would indicate an error.

Will it increase the load if i add error at every turn so that the script has more stuff to do?

Would you rather make sure you are checking for error conditions, or having a script run at the fastest speed possible until it crashes because of an error condition you didn't check for?

n.b. http://c2.com/cgi/wiki?PrematureOptimization

 

Edited by BrewManNH

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

Ok not sure im getting this across maybe

To sum up if the return code is just a simple one (non zero) then i dont need to be any more specific than

If @error then
;stuff done here

But if the item im checking for has a set of specific exit codes then this would be more appropiate

If $netsh1 < 0 Then
; stuff done here

;Or

If $netsh1 = 1 Then ;or 2 or -3 etc
; stuff done here

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I would either use a constant for the error code OR add a comment, because otherwise it will be harder for you to translate in the future e.g.

If @error = 99 Then ; 99 = Invalid user credentials

EndIf

; OR my personal favourite

Global Const $ONLINE_INVALID_CREDENTIALS = 99

If @error = $ONLINE_INVALID_CREDENTIALS Then

EndIf

But if you don't care about the specific error code, just that something when wrong, the your first snippet is fine.

Edited by guinness
Wrong comment token and initial comment

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

He already described his needs in the title: "error checking".
If a function sets the return value to denote an error then check the return value. If it sets @error/@extended then check this macros.
So: It depends on the way the called function returns an errror!


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

If he knew this, there would be no confusion and no thread.

A function can fail to produce your desired results, and not set @error, while the author can still deem it an error.

Error checking is not exclusive to @error.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

My understanding is that the OP wants to know if the called function returned an error. There are multiple ways to denote an error which are described in the help file (except for user written UDFs). This could be:

If the function fails the return value is set: Check the return value
If the function fails @error is set: Check @error
If the function fails anything else goes on: Check anything else ;)

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Reads to me like he's asking the best procedure to a variable question.

In some cases the function return will suffice, in others @error will, and others the specific value of @error is needed, while sometimes all might be required.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

That's it. And where do you find this information (except for user written UDFs)? In the Help File :)

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I would either use a constant for the error code OR add a comment, because otherwise it will be harder for you to translate in the future e.g.

But if you don't care about the specific error code, just that something when wrong, the your first snippet is fine.

I do comment heavily because i have a bad memory anyway and i always try not to use numbers without a variable

That what i needed to know thx

ps i saw you redacted earlier

Failure: sets the @error flag to non-zero.  so this then is anything but 0 (plus or minus)? presumably non zero would be minus?

Reads to me like he's asking the best procedure to a variable question.

In some cases the function return will suffice, in others @error will, and others the specific value of @error is needed, while sometimes all might be required.

No it was error checking, all im trying to do is write the correct type of error check in my scripts and not have to do it several times.

Its eeror checking for Shellexecutes and run etc i need to know if it fails and where

That's it. And where do you find this information (except for user written UDFs)? In the Help File :)

except i don't really understand the non zero bit which is why i made this thread what i was trying to get at is should i set it to look for a specific type of error or is general there is an error sufficient and/or if there is an error should i be checking for a negative event eg -1 or just accept that @error is enough by itself.

You should check for an error, the return code from the program being run may be anything, the error code is specific. Anything other than 0 would indicate an error.

Would you rather make sure you are checking for error conditions, or having a script run at the fastest speed possible until it crashes because of an error condition you didn't check for?

n.b. http://c2.com/cgi/wiki?PrematureOptimization

Thanks for the heavy reading, im glad im not a programmer sometimes lol, but i get your point

 

Thx for the input

Edited by Chimaera

Share this post


Link to post
Share on other sites

I took my comment away because I thought in the end you understood non-zero. So yeah, non-zero is 1, 2, 3.13, -99, basically anything that isn't zero.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

I think what you need to decide is:

do you just want to tell the user that an error (any error) occurred like: "Error occurred when executing ShellExecute. @error = 17, @extended = 0x80020009". Now the user has to decide how to interpret and handle this error.
I think this kind of error message is fine when the user is not responsible for the error (means by entering wrong data etc.). I would use this kind of error when other systems cause such errors (like SQL server is not running etc.).
I would present a message like this to the user:

"Retrieving data from SQL server failed.
Server name: xx, Connection string: ...,, executed query: ..., @error: nn, @extended: nn
Please call xxxx for assistance."
 

OR

when a problem can be caused by the user (like the users enters a filename and the file does not exist). In this case I would not only check for error/no error but analyze the error and post a meaningful message. Like:

"The filename you entered does not exist. Either the path or the filename are invalid. Please try again."


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

The error is for me or my lads that work in the shop only , no customers use my software

and for my instance it will be more like your first eg

Variable name: xx, Array error: ...,, @error: nn, "Array not created correctly"

 My problem stems from whilst the scripts i'm adding checking to work fine on win7 upwards, i get the occasional one that doesn't work on XP-Vista and i'm trying to write better scripts that leave a trail so i can find which bit caused the error.

Unfortunately some of the time its down to UDFs i use which are beyond my ability to fix.

So you can't win all the time, but i'm going to narrow it down so i can exactly say what it is that causes it.

Also down to the number of XP-Vista i see these days the odd script fail isn't a big problem anyway and will get less over time.

But i would still like to improve them to know for sure whats afoot.

Thx again

Share this post


Link to post
Share on other sites

Well in the specific instance of ShellExecuteWait() @error may no help you as no codes are documented, so you are relying on the exit code and you should see the documentation for whatever binary file you are using.

It is also worth calling _WinAPI_GetLastError() upon failure.

Also there are things you can to yourself if you wrap the function for example, if the verbs "edit" or "print" are used you could check if filename points to and executable file and know it will fail.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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