- I've tried to answer your question, do post more info if I haven't quite answered it.
- A function's return value is typically a numerical error(/success) code.
- But it can also be the value you want to return.
- E.g. ControlGetText could legitimately be a blank string, if the control is empty.
- FileGetAttrib returns the attributes for a file as letters, and this could legitimately be empty. (Note: FileExist does the same, but returns X for a file with no attributes, thus it always evaluates as true if a file exists.)
- To have both a return value and an error(/success) code, a function could return an error code via a ByRef function parameter, via ErrorLevel, or via SetLastError/A_LastError.
- One area of debate, is, when a function fails, whether to return a blank string, or whether to throw an error (which terminates the script, unless you use 'try'). I show some possibilities for error handling below.
Code: Select all
;q:: ;test error handling possibilities
vPath := A_ScriptFullPath
MsgBox, % MyFileGetSize(vPath)
MsgBox, % MyFileGetSizeAlt(vPath)
vPath := A_ScriptFullPath "x" ;non-existent file
MsgBox, % MyFileGetSize(vPath) ;if fail, return blank string and keep running script
try
MsgBox, % MyFileGetSizeAlt(vPath) ;if fail, keep running script (and run catch block if it exists)
catch
MsgBox, % "file not found:`r`n" vPath
MsgBox, % MyFileGetSizeAlt(vPath) ;if fail, terminate script
return
;==================================================
MyFileGetSize(vPath)
{
local
if !FileExist(vPath)
return ""
FileGetSize, vSize, % vPath
return vSize
}
MyFileGetSizeAlt(vPath)
{
local
if !FileExist(vPath)
throw Exception("file not found:`r`n" vPath, -1)
FileGetSize, vSize, % vPath
return vSize
}
;==================================================