Jump to content

Help with the error function


Recommended Posts

Hi Guys/gals,

 

I'm using this function and trying to utilise the error function in a try/catch situation

 

Func InputMatterNumber($MatterNumber)
   Local $hWnd1 = WinWait("[CLASS:ThunderRT6FormDC]", "", 10)
ControlSend($hWnd1, "", "ThunderRT6TextBox22", $MatterNumber)
     FileWrite($hFilehandle, @CRLF & $MatterNumber)
If @error = 1 Then
   MsgBox(($MB_ICONINFORMATION, "", "Cannot enter Matter number")
EndIf
Sleep(4000)
EndFunc

However I just ran it and it didn't send the matter number to the box but I didn't get the MsgBox which should've been triggered by the error function?

 

Cheers

Link to comment
Share on other sites

According to the help file: FileWrite does not set @error but the return value tells you if the command was successful or not ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

If you want to check if ControlSend did work or not then the check needs to be done immediately after ControlSend. Because each AutoIt function call resets @error.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Sure you get an error!

Global $handle
$iReturnvalue = FileWrite($handle, "Text")
If $iReturnvalue = 1 Then
    MsgBox(0, "Success!", "Data written to file!")
Else
    MsgBox(0, "Error!", "Data NOT written to file!")
EndIf

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

No, there's no reason except readability ;)

Your code can even be shortened to:

Quote
Global $handle 
If FileWrite($handle, "Text") Then
    MsgBox(0, "Success!", "Data written to file!")
Else
    MsgBox(0, "Error!", "Data NOT written to file!")
EndIf

 

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Or even shorter ;)

Global $handle
MsgBox(0, "Result", (FileWrite($handle, "Text") = 1) ? "Success!" : "Error!")

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

On 09/08/2018 at 10:19 AM, water said:

 Because each AutoIt function call resets @error.

@water : Hello. May I use this thread for a specific question related to this "@error reset when entering any Autoit function". Please have a look at the two following scripts  :

#include <MsgBoxConstants.au3>
Local $sFileRead = FileRead("C:\temp\this file name doesn't exist.txt")
If @error Then
   MsgBox(BitOR($MB_ICONERROR, $MB_TOPMOST), "1st script", "@error = " & @error)
Endif
#include <MsgBoxConstants.au3>
Local $sFileRead = FileRead("C:\temp\this file name doesn't exist.txt")
If @error Then
   MsgBox($MB_ICONERROR + $MB_TOPMOST, "2nd script", "@error = " & @error)
Endif

5b6ebccb420bd_Helpwiththeerrorfunction.jpg.25f9281db695536afe00af67e6d2b367.jpg

The 2nd script is correct (@error = 1) because the file to be read doesn't exist. So could we say that the MsgBox function in the 2nd script didn't reset @error to 0, but the BitOr() function in the 1st script did ?

I noticed Melba23 using a few times the + sign instead of BitOr(), so I tried his way and it works. My question : can we do this kind of thing with BitAnd() too ? I mean replacing BitAnd() by a mathematical operator that wont interfere with @error, as shown in the precedent scripts ?

Edited by pixelsearch
Link to comment
Share on other sites

In your case BitOr resets @error to 0 because statements are evaluated from left to right (this is true for BitAnd as well).

BitAnd won't work because:
BitOr($MB_ICONERROR, $MB_TOPMOST) returns 262160
BitAnd($MB_ICONERROR, $MB_TOPMOST) returns 262144

You could solve this problem with the approach you use in your 2nd script or by:

#include <MsgBoxConstants.au3>
Local $bFlag = BitOR($MB_ICONERROR, $MB_TOPMOST)
Local $sFileRead = FileRead("C:\temp\this file name doesn't exist.txt")
If @error Then MsgBox($bFlag, "1st script", "@error = " & @error)

or by

#include <MsgBoxConstants.au3>
Local $sFileRead = FileRead("C:\temp\this file name doesn't exist.txt")
Local $iError = @error
If $iError Then MsgBox(BitOR($MB_ICONERROR, $MB_TOPMOST), "1st script", "@error = " & $iError)

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks for your quick reply !

I started using your 2nd suggestion a few weeks ago, always keeping @error in a variable asap, like this (non runnable script) :

$iKeyIndex = _ArrayBinarySearch($aArray, $CheckSum, 0 ,0 ,1)
$iKeep_error = @error   ; important (+++) because any function (BitOr() in the following code) would reset @error to 0 when executed +++

If $iKeep_error = 0 Then
    MsgBox($MB_SYSTEMMODAL, 'Entry found', ' Index: ' & $iKeyIndex & "     @error = " & $iKeep_error)
Else    ; $iKeep_error <> 0
    MsgBox(BitOR($MB_ICONERROR, $MB_TOPMOST), 'Entry NOT found', ' Index: ' & $iKeyIndex & "     @error = " & $iKeep_error)
EndIf

And to make sure MsgBox resets @error to 0 even without BitOr() in it, here we go :

#include <MsgBoxConstants.au3>
Local $sFileRead = FileRead("C:\temp\this file name doesn't exist.txt")
If @error Then
   MsgBox($MB_ICONERROR + $MB_TOPMOST, "2nd script", "@error = " & @error)
   MsgBox($MB_ICONERROR + $MB_TOPMOST, "2nd script", "@error = " & @error)
Endif

It will show @error = 1 in 1st MsgBox.... immediately followed by @error = 0 in 2nd MsgBox :)

Thanks water !


 

Link to comment
Share on other sites

Glad I could shed some light onto the subject :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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