lua-users home
lua-l archive

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


To be precise, the documentation indicates that 'message' is not optional.
Thus the result of error() is undefined.

Thanks, this makes sense to me now. I received a similar answer on SO. 


The user of 'assert' wants to know where in the program it happened.
The user of 'error' with no message just wants abort the program. 
 
You can always do `error""` or `error"default text"` yourself.
 
Yeah but in my case I wanted to log an error via my own utility and then call `error()` to get stack trace output and terminate the program. Some parts of my code look like this:

if somethingWrong then
    myLogger:log(Level.Error, extraData, "this isn't looking good!")
    error() -- hope to also get stack trace without extra calls
end

But I'm probably making up a problem for myself...



On Fri, Aug 2, 2013 at 2:29 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2013/8/2 Dmitry Pashkevich <dpashk@gmail.com>:

> However, calling error() without arguments seems to make Lua die silently
> without printing stack trace.

> The documentation doesn't say anything about omitting the first message
> argument:
>
> error (message [, level])

To be precise, the documentation indicates that 'message' is not optional.
Thus the result of error() is undefined.

> I'm wondering if this is intended behavior or no?

I suspect that it is intended. It gives you something not achievable by
any non-nil message. Nothing printed, but the OS can detect the error.

    $ if `lua -e ''`; then echo 'no error'; else echo 'error'; fi
    no error
    $ if `lua -e 'error()'`; then echo 'no error'; else echo 'error'; fi
    error

> IMO it would make sense to
> still print stack trace (and maybe output some default text e.g. error) even
> if no message is provided, because that's how theassert() function works.

The situation is different. 'error' is unconditional, 'assert' is conditional.
The user of 'assert' wants to know where in the program it happened.
The user of 'error' with no message just wants abort the program.

You can always do `error""` or `error"default text"` yourself.




--
Dmitry Pashkevich