Error handling

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

Error handling

Robert Hibberdine
Hi all,

I am writing my first large lua script to run on an embedded system and need to handle errors in my own way. I have just discovered 'pcall'...

My question is:
How do I print the function name and line number where the error occurred in my own error handler?

TIA

Bob


Reply | Threaded
Open this post in threaded view
|

RE: Error handling

Paul Hudson-2
Look at xpcall.

http://www.lua.org/manual/5.1/manual.html#pdf-xpcall

http://www.lua.org/pil/8.5.html


-----Original Message-----
From: [hidden email]
[[hidden email]] On Behalf Of Robert Hibberdine
Sent: 10 May 2006 09:51
To: Lua list
Subject: Error handling

Hi all,

I am writing my first large lua script to run on an embedded system and 
need to handle errors in my own way. I have just discovered 'pcall'...

My question is:
How do I print the function name and line number where the error 
occurred in my own error handler?

TIA

Bob




Reply | Threaded
Open this post in threaded view
|

Re: Error handling

Mildred Ki'Lya
In reply to this post by Robert Hibberdine
On Wed, 10 May 2006 09:51:14 +0100 Robert Hibberdine wrote:
> Hi all,
> 
> I am writing my first large lua script to run on an embedded system
> and need to handle errors in my own way. I have just discovered
> 'pcall'...
> 
> My question is:
> How do I print the function name and line number where the error 
> occurred in my own error handler?

If you are interested, I made a exception module for lua 5.1
To use it :
	require("exception").install()

then you can use :
	raise(exception_id, message)
	try(function()
		-- some code that may raise exceptions
	end, function(e)
		if e.error == exception_id then
			-- do something
			return true
		end
		return false -- the exception continues
	end

It's here:

http://bzr.mildred632.free.fr/viewsource/Projects/lua/mymodules-5.1/exception.lua?action=view
or
http://bzr.mildred632.free.fr/Projects/lua/mymodules-5.1/exception.lua


Mildred
-- 
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]


-- 
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]

Reply | Threaded
Open this post in threaded view
|

Re: Error handling

Robert Hibberdine

Thank you for that.
Does it work with Lua 5.0? (I use v5.0 because I am using CGILua on my system) At present I am not competent enough in Lua to eitther understand what you've done or back port it to v5.0 if necessary :-)).

Thanks
Bob

Mildred wrote:

On Wed, 10 May 2006 09:51:14 +0100 Robert Hibberdine wrote:
Hi all,

I am writing my first large lua script to run on an embedded system
and need to handle errors in my own way. I have just discovered
'pcall'...

My question is:
How do I print the function name and line number where the error occurred in my own error handler?

If you are interested, I made a exception module for lua 5.1
To use it :
	require("exception").install()

then you can use :
	raise(exception_id, message)
	try(function()
		-- some code that may raise exceptions
	end, function(e)
		if e.error == exception_id then
			-- do something
			return true
		end
		return false -- the exception continues
	end

It's here:

http://bzr.mildred632.free.fr/viewsource/Projects/lua/mymodules-5.1/exception.lua?action=view
or
http://bzr.mildred632.free.fr/Projects/lua/mymodules-5.1/exception.lua


Mildred


Reply | Threaded
Open this post in threaded view
|

Re: Error handling

Petite Abeille
In reply to this post by Robert Hibberdine

On May 10, 2006, at 10:51, Robert Hibberdine wrote:

How do I print the function name and line number where the error occurred in my own error handler?

In general, you can always discover such information using debug.getinfo():

        local someInfo = debug.getinfo( 3, "Sn" )
        local aClass = someInfo.source
        local aMethod = someInfo.name

http://www.lua.org/pil/23.1.html
http://www.lua.org/manual/5.1/manual.html#pdf-debug.getinfo

There is also a handy debug.traceback to use in conjunction with xpcall:

http://www.lua.org/manual/5.1/manual.html#pdf-debug.traceback

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/


Reply | Threaded
Open this post in threaded view
|

Re: Error handling

Mildred Ki'Lya
In reply to this post by Robert Hibberdine
On Wed, 10 May 2006 18:44:12 +0100 Robert Hibberdine wrote:
> 
> Thank you for that.
> Does it work with Lua 5.0? (I use v5.0 because I am using CGILua on
> my system)
> At present I am not competent enough in Lua to eitther understand
> what you've done or back port it to v5.0 if necessary :-)).

Sorry, I'm late, I do not look at the list frequently these times.
And sorry, it is not lua 5.0 compatible.

Try the file I send now, I didn't tested it but it should work, I
think.

-- 
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]

Attachment: exception50.lua
Description: Binary data