Noob question -- On error, exit function and terminate script

classic Classic list List threaded Threaded
26 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Noob question -- On error, exit function and terminate script

Paul E. Merrell, J.D.
Hi, all,

I've spent a couple of hours now looking for the answer without
success. Is there a simple method to terminate a script upon an error
condition that occurs within a function, E.g.,

function foo(bar)
   if bar == ""
      ???
   end
   ...
end

Appreciate any guidance here.

Best regards,

Paul

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Xavier Wang

maybe you can try os.exit()

在 2011-6-25 晚上9:23,"marbux" <[hidden email]>写道:
> Hi, all,
>
> I've spent a couple of hours now looking for the answer without
> success. Is there a simple method to terminate a script upon an error
> condition that occurs within a function, E.g.,
>
> function foo(bar)
> if bar == ""
> ???
> end
> ...
> end
>
> Appreciate any guidance here.
>
> Best regards,
>
> Paul
>
Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Rebel Neurofog
In reply to this post by Paul E. Merrell, J.D.
assert () is the best in your case (more verbose)

error () may also be helpful
especially if your error message requires complex evaluation:
-- This requires 'some_complex_call ()' even if there was no error:
assert (bar ~= "", "Error: "..some_complex_call ())

-- While this will call 'some_complex_call ()' only in case of error
if bar == "" then
   error ("Error: "..some_complex_call ())
end

On Sat, Jun 25, 2011 at 5:22 PM, marbux <[hidden email]> wrote:

> Hi, all,
>
> I've spent a couple of hours now looking for the answer without
> success. Is there a simple method to terminate a script upon an error
> condition that occurs within a function, E.g.,
>
> function foo(bar)
>   if bar == ""
>      ???
>   end
>   ...
> end
>
> Appreciate any guidance here.
>
> Best regards,
>
> Paul
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Gavin Wraith
In reply to this post by Xavier Wang
In message <BANLkTi=[hidden email]> you wrote:

> > I've spent a couple of hours now looking for the answer without
> > success. Is there a simple method to terminate a script upon an error
> > condition that occurs within a function,

Diego Nehab's

http://lua-users.org/wiki/FinalizedExceptions

might be relevant.
 
--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Xavier Wang
In reply to this post by Rebel Neurofog


2011/6/25 Rebel Neurofog <[hidden email]>
assert () is the best in your case (more verbose)

error () may also be helpful
especially if your error message requires complex evaluation:
-- This requires 'some_complex_call ()' even if there was no error:
assert (bar ~= "", "Error: "..some_complex_call ())

-- While this will call 'some_complex_call ()' only in case of error
if bar == "" then
  error ("Error: "..some_complex_call ())
end
do we have simple ASSERT like thing to do this automatic?
just like macro or other thing...

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Timothy Hunter


On Jun 25, 2011, at 11:17 AM, Xavier Wang wrote:



2011/6/25 Rebel Neurofog <[hidden email]>
assert () is the best in your case (more verbose)

error () may also be helpful
especially if your error message requires complex evaluation:
-- This requires 'some_complex_call ()' even if there was no error:
assert (bar ~= "", "Error: "..some_complex_call ())

-- While this will call 'some_complex_call ()' only in case of error
if bar == "" then
  error ("Error: "..some_complex_call ())
end
do we have simple ASSERT like thing to do this automatic?
just like macro or other thing...


Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Xavier Wang


2011/6/25 Timothy Hunter <[hidden email]>


On Jun 25, 2011, at 11:17 AM, Xavier Wang wrote:



2011/6/25 Rebel Neurofog <[hidden email]>
assert () is the best in your case (more verbose)

error () may also be helpful
especially if your error message requires complex evaluation:
-- This requires 'some_complex_call ()' even if there was no error:
assert (bar ~= "", "Error: "..some_complex_call ())

-- While this will call 'some_complex_call ()' only in case of error
if bar == "" then
  error ("Error: "..some_complex_call ())
end
do we have simple ASSERT like thing to do this automatic?
just like macro or other thing...


Sorry, but I mean, a new version of assert against the baselib one that _don't_ evaluate the second expression if the first expression is true.
Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

steve donovan
On Sat, Jun 25, 2011 at 6:18 PM, Xavier Wang <[hidden email]> wrote:
> Sorry, but I mean, a new version of assert against the baselib one that
> _don't_ evaluate the second expression if the first expression is true.

That is difficult because assert() is a function, not a macro like in C.

It's a pity that expressions cannot be statements, e.g. the expression

1 == 0 or error_fn()

works as we want: error_fn() is only called if the first condition fails.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Xavier Wang


在 2011-6-26 半夜12:41,"steve donovan" <[hidden email]>写道:
>
> On Sat, Jun 25, 2011 at 6:18 PM, Xavier Wang <[hidden email]> wrote:
> > Sorry, but I mean, a new version of assert against the baselib one that
> > _don't_ evaluate the second expression if the first expression is true.
>
> That is difficult because assert() is a function, not a macro like in C.
>
> It's a pity that expressions cannot be statements, e.g. the expression
>
> 1 == 0 or error_fn()
>
> works as we want: error_fn() is only called if the first condition fails.
Thank you! That is new to me, and very useful!!

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Lorenzo Donati-2
In reply to this post by Xavier Wang
On 25/06/2011 18.18, Xavier Wang wrote:

[snip]

> Sorry, but I mean, a new version of assert against the baselib one that
> _don't_ evaluate the second expression if the first expression is true.

I don't think this is possible without altering deeply the semantics of
the language, since "assert" is a regular function (although implemented
in C), so all its arguments are evaluated before the call.

The possible solutions I see both involve change in the language:

1. turn "assert" into some sort of operator, which its kind of short
circuit evaluation (like "and" and "or").

2. introduce a way of telling Lua to delay the evaluation of an
expression (this has been proposed before IIRC).

I wouldn't want the first (too specific). Maybe the second could have
its applications if done well and it may be general enough to fit Lua's
spirit.

P.S.: note that you can achieve sort of "short circuited" assert writing:

local _ = condition or error( msg )

instead of

assert( condition, msg )

But I don't like that idiom too much. It doesn't save too much typing
compared to:

if not condition then error( msg ) end

and it is less explicit (i.e. unreadable ).


-- Lorenzo

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Xavier Wang


2011/6/26 Lorenzo Donati <[hidden email]>
On 25/06/2011 18.18, Xavier Wang wrote:

[snip]


Sorry, but I mean, a new version of assert against the baselib one that
_don't_ evaluate the second expression if the first expression is true.

I don't think this is possible without altering deeply the semantics of the language, since "assert" is a regular function (although implemented in C), so all its arguments are evaluated before the call.

The possible solutions I see both involve change in the language:

1. turn "assert" into some sort of operator, which its kind of short circuit evaluation (like "and" and "or").

2. introduce a way of telling Lua to delay the evaluation of an expression (this has been proposed before IIRC).

I wouldn't want the first (too specific). Maybe the second could have its applications if done well and it may be general enough to fit Lua's spirit.

P.S.: note that you can achieve sort of "short circuited" assert writing:

local _ = condition or error( msg )

instead of

assert( condition, msg )

But I don't like that idiom too much. It doesn't save too much typing compared to:

if not condition then error( msg ) end

and it is less explicit (i.e. unreadable ).


-- Lorenzo

Some Idea:
may be the last one statement of a block maybe a expression, so we can write:
do condition or error( msg ) end

this may better.

I like the idea of lazy evaluation, also.
Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Rebel Neurofog
In reply to this post by Xavier Wang
> do we have simple ASSERT like thing to do this automatic?
> just like macro or other thing...

Actually I would be glad if assert () would do this:
assert (bar ~= "", function () return "Error: "..some_complex_call () end)

But it doesn't: it only takes a string as a second argument. Even in Lua 5.2.
Sounds like a good proposal...

But for now it can only be emulated like that (as far as I know):

function assert2 (expr, message, ...)
   if expr then
      return expr, message, ...
   end
   if type (message) == "function" then
      return assert (expr, message (), ...)
   end
   return assert (expr, message, ...)
end

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Lorenzo Donati-2
In reply to this post by Xavier Wang
On 25/06/2011 18.58, Xavier Wang wrote:

[snip]

> Some Idea:
> may be the last one statement of a block maybe a expression, so we can
> write:
> do condition or error( msg ) end

IIRC one of the objection of this approach (or similar ones proposed for
"expression as statements") is that they confuse the parser, but
I'm not an expert, so I cannot comment on this.

>
> this may better.
>
> I like the idea of lazy evaluation, also.


Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Xavier Wang


2011/6/26 Lorenzo Donati <[hidden email]>
On 25/06/2011 18.58, Xavier Wang wrote:

[snip]


Some Idea:
may be the last one statement of a block maybe a expression, so we can
write:
do condition or error( msg ) end

IIRC one of the objection of this approach (or similar ones proposed for "expression as statements") is that they confuse the parser, but
I'm not an expert, so I cannot comment on this.


if the expression only occur on the last position of block, is there still has confusion? 
 
 
Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Florian Weimer
In reply to this post by Rebel Neurofog
* Rebel Neurofog:

> assert () is the best in your case (more verbose)
>
> error () may also be helpful
> especially if your error message requires complex evaluation:
> -- This requires 'some_complex_call ()' even if there was no error:
> assert (bar ~= "", "Error: "..some_complex_call ())
>
> -- While this will call 'some_complex_call ()' only in case of error
> if bar == "" then
>    error ("Error: "..some_complex_call ())
> end

You can define something like this:

function fassert(condition, f, ...)
   if not condition then
      error("Error: " .. f(...))
   end
end

(The "Error: " prefix is actually unnecessary, I think.)

Then you can write:

  fassert(bar ~= "", some_complex_call)

Or if some_complex_call takes any arguments:

  fassert(bar ~= "", some_complex_call, arg1, arg2)

Perhaps this is good enough for your purpose?

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Paul E. Merrell, J.D.
In reply to this post by Rebel Neurofog
On Sat, Jun 25, 2011 at 7:12 AM, Rebel Neurofog <[hidden email]> wrote:
> assert () is the best in your case (more verbose)

Thank you, Rebel, and the others who responded; assert works for my use case.

BTW, I suggest that Reference Manual section 5.1 be amended to
indicate that assert and error will cause termination of the script
when their evaluations indicate an error condition. That information
is currently not included in their respective documentation sections.
See <http://www.lua.org/manual/5.1/manual.html#pdf-assert> and
<http://www.lua.org/manual/5.1/manual.html#pdf-error>.

Best regards,

Paul

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Lorenzo Donati-2
On 25/06/2011 20.35, marbux wrote:
> On Sat, Jun 25, 2011 at 7:12 AM, Rebel Neurofog<[hidden email]>  wrote:
>> assert () is the best in your case (more verbose)
>
> Thank you, Rebel, and the others who responded; assert works for my use case.
>
> BTW, I suggest that Reference Manual section 5.1 be amended to
> indicate that assert and error will cause termination of the script
> when their evaluations indicate an error condition.

This is not necessarily true. Citing from "error" entry in the manual:

"Terminates the last protected function called and returns message as
the error message."

If "error" is called inside a function called with pcall or xpcall, the
script doesn't terminate.

As for the relevance of that sentence: a script is nothing more than a
chunk (i.e. a function) loaded from a file, so if it is run in protected
mode, it doesn't terminate.

For example (untested code):

-- script1.lua
local chunk = assert( loadfile "script2.lua" )

pcall( chunk )

print "chunk broken, but I'm still alive!"

-- script2.lua
error "I'm broken!!!"

-----------------------------


>That information
> is currently not included in their respective documentation sections.
> See<http://www.lua.org/manual/5.1/manual.html#pdf-assert>  and
> <http://www.lua.org/manual/5.1/manual.html#pdf-error>.
>
> Best regards,
>
> Paul
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Lorenzo Donati-2
In reply to this post by Paul E. Merrell, J.D.
On 25/06/2011 20.35, marbux wrote:

> On Sat, Jun 25, 2011 at 7:12 AM, Rebel Neurofog<[hidden email]>  wrote:
>> assert () is the best in your case (more verbose)
>
> Thank you, Rebel, and the others who responded; assert works for my use case.
>
> BTW, I suggest that Reference Manual section 5.1 be amended to
> indicate that assert and error will cause termination of the script
> when their evaluations indicate an error condition. That information
> is currently not included in their respective documentation sections.
> See<http://www.lua.org/manual/5.1/manual.html#pdf-assert>  and
> <http://www.lua.org/manual/5.1/manual.html#pdf-error>.
>

I must nevertheless admit that the description for assert is slightly vague:

"Issues an error when the value of its argument v is false (i.e., nil or
false);..."

I'll suggest to change it to:

"Calls the function 'error' when the value of its argument v is false
(i.e., nil or false);..."

So that one avoids being mislead to think that the error mechanism could
be different from that of "error".

This is not nitpicking, since assertions in Java and other languages
aren't part of the normal error management system, unlike in Lua.

Maybe a less misleading name should have been chosen ("ensure", "test",
"check" as in the C API?), but now it's too late.

> Best regards,
>
> Paul
>
>
>
-- Lorenzo


Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Paul E. Merrell, J.D.
On Sat, Jun 25, 2011 at 12:19 PM, Lorenzo Donati
<[hidden email]> wrote:

> I must nevertheless admit that the description for assert is slightly vague:
>
> "Issues an error when the value of its argument v is false (i.e., nil or
> false);..."
>
> I'll suggest to change it to:
>
> "Calls the function 'error' when the value of its argument v is false (i.e.,
> nil or false);..."
>
> So that one avoids being mislead to think that the error mechanism could be
> different from that of "error".

I'd like to see something there about the function being terminated.
Perhaps, embellishing your suggested text:

"Terminates the last protected function called and calls the function
'error' when the value of its argument v is false (i.e., nil or
false);..."

And perhaps add something like: "The most recent protected call will
contain the error and prevent it from propagating further."[1]

But please take into account that I don't know what I'm writing about. :-)

Best regards,

Paul

[1] Borrowed from K. Jung and A. Brown, Beginning Lua Programming
(2007), pg. 333.

Reply | Threaded
Open this post in threaded view
|

Re: Noob question -- On error, exit function and terminate script

Luiz Henrique de Figueiredo
In reply to this post by Rebel Neurofog
> But for now it can only be emulated like that (as far as I know):
>
> function assert2 (expr, message, ...)
>    if expr then
>       return expr, message, ...
>    end
>    if type (message) == "function" then
>       return assert (expr, message (), ...)
>    end
>    return assert (expr, message, ...)
> end

See also http://lua-users.org/lists/lua-l/2010-05/msg00494.html

12