9

Is there any way to simulate a try-finally or try-except in a language that doesn't have them?

If there's some random, unpredictable, exception happens i need to be sure some cleanup runs.

i could try to be sure that no exception in thrown, that way i am sure my cleanup code always runs - but then i wouldn't need the try-finally/except.

Right this moment i'm trying to create a try-finally in Lua; but i think any solution would work in other languages as well.

Although, for the life of me, i cannot figure out how an exception can be handled without the plumbing provided by the language infrastructure.

But never hurts to ask.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Is the "assert" function not an option? http://www.lua.org/pil/8.3.html – Will Jan 02 '12 at 02:25
  • @Will There's no problem with exceptions being thrown, either by me or *not* by me. i just need to stop them. Can `assert` do that? – Ian Boyd Jan 02 '12 at 02:28
  • Yes, I believe assert will prevent exceptions from being thrown and allow you to raise your own exception instead. Oh, and what Gerald posted is another good function too, pcall. I'm not too familiar with error handling in Lua, but I believe there are a few functions that'll do what you want. – Will Jan 02 '12 at 02:30
  • @Will i read it as the opposite: "`assert(io.open(name, "r"));` *This is a typical Lua idiom: If io.open fails, assert will raise an error.*" – Ian Boyd Jan 02 '12 at 02:32
  • Ah, I think I am mistaken then, but my friends down in the answers section seem to have some good ideas :-) – Will Jan 02 '12 at 02:34
  • Your wording is a little confusing; you're _really_ asking about Lua but you start out asking about languages in general (which is an amazingly tricky problem, really). It'd be cool if you edited your question to make it clearer that you're asking about how to do it with Lua only. – Donal Fellows Jan 02 '12 at 02:50
  • @DonalFellows i *assumed* any solution given in Lua would also work in other languages. Turns out i may be wrong - but i was hoping there some ancient secret to catching errors from the olden-times that has been lost to the mists of time. – Ian Boyd Jan 02 '12 at 03:03

3 Answers3

15

Lua already has the necessary mechanisms to do something not entirely unlike exceptions. Namely pcall.

You can use pcall to execute any Lua function. If that function (or any function it calls) calls error (assert calls error if the assertion condition is not true), then flow control will return to the site of the pcall statement. The pcall will return false and an error message (what is passed to error).

With this, you can "throw" errors and "catch" them. Your "try" is just the pcall; your "catch" statement is what checks the pcall result.

Also, remember: Lua is a garbage collected environment. You shouldn't need to do any cleanup work. Or if you do, you need to change whatever Lua module requires it. Lua APIs should be Lua APIs, not C or C++ APIs.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • In this case *"cleanup"* meant: `suppressFlag++; pcall(doStuff); suppressFlag--;`. And it looks like my original fear was right - without language support catching arbitrary errors is not a possibility. But excellent work on finding `pcall`. i googled for about 5 minutes and couldn't find anything. – Ian Boyd Jan 02 '12 at 03:04
  • I know this answer is old, but I must say I disagree with your last paragraph. File handles, database connections, and all sorts of non-memory resources aren't garbage-collected. It's what `finally`-like constructs are for in other garbage collected languages. – Not a real meerkat Sep 16 '18 at 13:49
  • What about Lua 4? It does not have a `pcall` function. – posfan12 Sep 30 '20 at 00:09
  • @posfan12: What about it? Lua 4 dates back to 2001, with Lua 5 coming out a scant 3 years later. It's too old to care about for most applications, and those where you have to use it will have to live within its limitations. – Nicol Bolas Sep 30 '20 at 01:11
2

Never programmed in lua (which is what you have this tagged as). However, several web pages including this one http://jessewarden.com/2011/01/lua-for-actionscript-developers.html mentioned that protected call (pcall) is lua error handling device.

Hope this helps.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
1

How about this Lua Exception Handling system? You can also use Lua RAII mechanisms.

Keldon Alleyne
  • 2,103
  • 16
  • 23