Sign in to follow this  
Followers 0
Oldschool

Properly using @Error in functions

4 posts in this topic

Can somebody point me in the right direction with this...

For instance this function, how do I clip it through with "If @Error" properly:

Func InetGetNow()
    Local $sTimeSource = _INetGetSource("http://www.time.gov/timezone.cgi?Eastern/d/-5")
    Local $sBR = StringRegExp($sTimeSource, '(?i)<font size="5" color="white">(.*)<br>', 3)
    Return $sBR[0]
EndFunc

And any other exaples are welcome

Share this post


Link to post
Share on other sites



Can somebody point me in the right direction with this...

For instance this function, how do I clip it through with "If @Error" properly:

Func InetGetNow()
    Local $sTimeSource = _INetGetSource("http://www.time.gov/timezone.cgi?Eastern/d/-5")
    Local $sBR = StringRegExp($sTimeSource, '(?i)<font size="5" color="white">(.*)<br>', 3)
    Return $sBR[0]
EndFunc

And any other exaples are welcome

I prefer this kind of thing (note I changed the function name to _INetGetDate(), since you changed with the RegExp and it doesn't return time with the date now):

#include <INet.au3>

$sTime = _INetGetDate()
If @error Then
    MsgBox(16, "Error", "_INetGetDate() failed, @error = " & @error & ", @extended = " & @extended)
Else
    MsgBox(64, "Success", "_INetGetDate() returned: " & $sTime)
EndIf

; On success returns date string
; On failure returns 0 and sets @error:
;   @error = 0  Not error
;   @error = 1  _INetGetSource() failed, @extended = @error returned by that function
;   @error = 2  StringRegExp() failed, @extended = @error returned by that function
Func _INetGetDate()
    Local $sTimeSource = _INetGetSource("http://www.time.gov/timezone.cgi?Eastern/d/-5")
    If @error Then Return SetError(1, @error, 0)
    
    Local $sBR = StringRegExp($sTimeSource, '(?i)<font size="5" color="white">(.*)<br>', 3)
    If @error Then
        Return SetError(2, @error, 0)
    Else
        Return $sBR[0]
    EndIf
EndFunc   ;==>_INetGetDate

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Nicely done...

So my assumption is that you can add the "If @ error" conditional statement to any function.

Share this post


Link to post
Share on other sites

Nicely done...

So my assumption is that you can add the "If @ error" conditional statement to any function.

Hopefully that space in @error is just a typo. When any function is called in AutoIt, @error is forced to 0 by default and remains 0 unless changed by the function (or a function called within the function). The conditional test "If @error Then" checks @error for ANY other value besides 0, because any non-zero integer is a boolean TRUE for AutoIt.

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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
Sign in to follow this  
Followers 0