lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 03/05/2017 06:40 PM, Xavier Wang wrote:
It should be:

assert(io.open("results.txt","w")):write(table.concat(results,"\n")):close()

when you use in production code.

This should *not* be used in production since it does not check for error return from file:write() or file:close(). Try this:

assert(assert(assert(io.open("results.txt","w")):write(table.concat(results,"\n"))):close());

See also: http://lua-users.org/lists/lua-l/2011-03/msg00290.html

This is precisely why exceptions were created, because most (i.e. nearly all) programmers do not properly handle errors when they are mixed into the data channel of the return-value. It was always a favorite parlor game of mine, when I encounter a C programmer who insists that he checks for errors in the return-value of _every_ function call that he makes, to show me all of his printf()s and fprintf()s. I've not lost that bet yet.