lua-users home
lua-l archive

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


mailing lists are cool, i just realized, that the input of assert is
just like the return of those functions, which may return nil + error
:D (and i've just learnt where to use 'may' instead of 'can', so this
is the very 1st time i've ever used it, and i'm proud of it :D )

2017-03-06 4:10 GMT+01:00 David Favro <lua@meta-dynamic.com>:
> 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.
>
>