Sign in to follow this  
Followers 0
czardas

Which Error Return Is Best

22 posts in this topic

#1 ·  Posted (edited)

I have a function which operates on numbers.One digit is not allowed in the input and it's occurence should throw an error. I could return the input number (without changes, since it is an illegal numeric parameter to start off with) or I could return an empty string. I am leaning towards returning the original number unchanged, since there is less of a chance that subsequent numeric functions will throw further errors. Bear in mind that it is down to the person using the function to check for errors. I just wanted to ask people's opinions about this. Would you return the original number?

Edit

For some reason (perhaps it's me or a perhaps it returns TRUE automatically), the function is returning 1 when I try to avoid returning a value. That must not be allowed, so I have to make a choice of some kind!

See for yourself

MsgBox(0, "No Return", _MyRet(7))

Func _MyRet($iInt)
    ; Something here
    Return SetError(6)
EndFunc
Edited by czardas

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

czardas,

I would argue that trying to create "less of a chance that subsequent numeric functions will throw further errors" is not the way you should be thinking. If the user has no errorchecking in place and misses the erroneous return, allowing other functions to run with an "illegal numeric parameter" will just confuse matters further and make it even harder to determine where the error actually occurred :D.

I would go for a hard fail return, with @error set and a non-numeric return using something like:

ConsoleWrite(_Func() & @CRLF & @error & @CRLF & @extended & @CRLF)
Func _Func()
    Return SetError(99, 77, "Wrong")
EndFunc

By the way, functions return 0 unless you ask then to do otherwise: :oops:

ConsoleWrite(_Func() & @CRLF)
Func _Func()
EndFunc

M23

Edit: I have just seen your edit above. You get a return of 1 because it is being set by a successful SetError call - that is why there is a specific parameter within SetError to allow you to return another value. :rip:

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I'm glad I asked. Thanks for the useful information. :oops:

Sometimes I think I have my head on back to front.

:D

MsgBox(0, "", 0/0)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

In my UDFs the value I return in case of an error depends on the dataype the function returns when no error occurred.

0: when a numeric value or an object is expected

"": when a string or array is expected

It's the users responsibility to check that no error occurred so he can process the returned data savely.

Edited by water

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

hello czardas;

I understood that you want to return same value of parameter

try to use this:

MsgBox(0,"","the return value is " & somefunc(5))
Func somefunc($var)
;some code
Return SetError(@error,0,$var) ; it returns same passed parameter
EndFunc

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks bestsmr, but that's what I had already. I'm going to change it now after Melba's insightful comments. And Water, I can't return 1 or -1 since these are valid return values without an error. So it will have to be a string. Something better than -1.#IND :D

Edited by czardas

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

For some reason (perhaps it's me or a perhaps it returns TRUE automatically), the function is returning 1 when I try to avoid returning a value. That must not be allowed, so I have to make a choice of some kind!

See for yourself

MsgBox(0, "No Return", _MyRet(7))

Func _MyRet($iInt)
    ; Something here
    Return SetError(6)
EndFunc
That's because every AutoIt function (in this case SetError) returns 1 on success. So the statement
Return SetError(6)
evaluates to
Return 1
Edited by water

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

And Water, I can't return 1 or -1 since these are valid return values without an error. So it will have to be a string.

1 and -1 aren't valid return values from a function unless you set them to that when you return from that function. If you're creating the function, you determine what the return values are and check for them accordingly. If your function has an error and you decide that returning 1 indicates that there was an error, then that's what the 1 means for that function. There is no default return values from a function other than 0.

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

Most of the time I return a valid result. If the user doesn't check @error he can't be sure that the return value is correct.

You denote that an error occurred in two places: @error and the return value.

I think to set @error is enough to tell the user that there was a problem. The user has to check @error.

Because if the user doesn't check the return value his script will crash when further processing the returned data.


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

#11 ·  Posted (edited)

water, that was one of my original concerns. I have a number of functions all obeying the same error checking rules and I have a separate function to do the checking. If the checks fail, then the main function rejects the parameter. Keeping the same parameter would enable the script to continue throwing the same error on the same parameter in different functions, but it won't ever hard crash autoit. Funny kind of logic maybe. Dunno! It's not really a big deal, but I appreciate the responces.

Edited by czardas

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I tried returning a string but the same issues occur.

MsgBox(0, "Hello World plus zero", _MyRet(7) + 0)

Func _MyRet($iInt)
    ; Something here
    Return SetError(6, 0, "Hello World")
EndFunc

Setting the error value equal to infinity seems to work quite well. :oops:

;Example1
MsgBox(0, "NaN", _MyRet(7) + 0) ; infinity + 0

;Example2
Dim $NaN = _MyRet(27)
If Not (IsInt($NaN) And IsString($NaN)) Then MsgBox(0, "Infinity", $NaN & " WTF?")

Func _MyRet($iInt)
    ; Something here
    Return SetError(6, 0, 1/0)
EndFunc

I doubt that it's a good idea though. :D

Edited by czardas

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Illustration of why it's not a good idea to set infinity as a (hard error) return value. :oops:

HotKeySet("{ESC}", "_Quit")

Dim $infinity = _MyRet()
For $i = 0 To $infinity
    MsgBox(0, "Loop Number " & $i, "Hit escape to quit")
Next

Func _MyRet()
    ; Something here
    Return SetError(1, 0, 1/0)
EndFunc

Func _Quit()
    Exit
EndFunc

i.e. Infinity is still interpreted as a valid number.

If IsNumber(1/0) Then MsgBox(0, "", 1/0 & " is a number")

The next logical step would be to try using the NUL character.

If IsNumber("" + 0) Then MsgBox(0, "Hard Error", "Nope!")
If Not (IsNumber(_MyRet()) And IsNumber(_MyRet() + 0)) Then MsgBox(0, "Hard Error", "Yep!")

Func _MyRet()
    ; Something here
    Return SetError(1, 0, Chr(0))
EndFunc

This idea is probably bad, since I imagine it will cause AutoIt to crash under certain unforseen circumstances.

:D Maybe I should decide what to do by tossing a coin.

Edited by czardas

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

... The next logical step would be to try using the NUL character.

This idea is probably bad, since I imagine it will cause AutoIt to crash under certain unforseen circumstances.

Bad: yes.

Crash: unlikely, but easy to test ...

Issue: not all string related functions act the same way on the Null-character.

Just keep it simple.

Empty string to indicate a empty none-index array.

Zero or something like -1 for number failures if allowed by function. (ergo: if Zero/-1/etc is not part of the valid return range.)

Rest ... @error.

Only real case where using the function return data as a success or failure indicator is if the function is not returning (by way of Return) any (other) data. (Bool or ByRef type function.)

Now go KISS and make up with your Code. :D

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Now go KISS and make up with your Code. :oops:

:D

The ONLY numeric return values that are not legitimate are the erroneous ones passed to the function in the first place. 0 and 1 and perfectly legitimate results. Anyway, I know what to do now after the opinions expressed. Thanks again.

Share this post


Link to post
Share on other sites

Return the "simpliest" non-legitimate value and set @error to non-zero.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

czardas,

Sounds very like the advice I gave you back in post #2! :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Yeah Melba!

I discover a lot of new things by trying out different alternatives. Sometimes I feel a bit stupid asking dumb questions, but I don't mind too much if others a can benefit from reading this. It's good to get second opinions. :D

Share this post


Link to post
Share on other sites

I would go for a hard fail return, with @error set and a non-numeric return using something like:

ConsoleWrite(_Func() & @CRLF & @error & @CRLF & @extended & @CRLF)
Func _Func()
    Return SetError(99, 77, "Wrong")
EndFunc

Small Question

So your saying we should always set a return in a func?

and what do you use the 77 for which i think is the @extended for?

That's because every AutoIt function (in this case SetError) returns 1 on success. So the statement

Return SetError(6)
evaluates to
Return 1

Didnt know this at all, so if i made a function or whatever and it gives 1 instead of what i asked it to do ,its my bit of the function, ie the thing asked it to do that is wrong the func is correct because it returns 1?

1 and -1 aren't valid return values from a function unless you set them to that when you return from that function. If you're creating the function, you determine what the return values are and check for them accordingly. If your function has an error and you decide that returning 1 indicates that there was an error, then that's what the 1 means for that function. There is no default return values from a function other than 0.

Sorry im confused now so were saying 1 isnt valid now?

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