lua-users home
lua-l archive

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


In my point of view, an exception is potentially what the name says: An Exception, something unusual and probably unexpected. Definitely (yet in  my point of view) it's a good design prepare your code for unexpected cases.

In a simple example of file transfer over the internet, unexpected things can occur in the middle of the process and it is a good pratice ensure that the "file handle" will be closed at the end no matter what. In these case, a try-finally statement would be very useful.

Probably we have different point of view of what is a good code design when facing exceptions and unexpected situations.

On Tue, May 31, 2016 at 8:17 AM, Jonne Ransijn <yoyoyonny@gmail.com> wrote:

I do not think this will ever be added to Lua.

Exceptions should be used as exactly that: Edge cases wherein the requested action cannot be completed.

Some people in the Java and C# community seem to disagree, but in case of an exception the program should crash. There is NO valid reason to ever catch a PathTooLongException or AssertException, except at the very top of the program, to print an error message.

Furthermore, THROWING an exception is usually bad design. If you do throw, make sure your case is ACTUALLY an exception, not just something that you don't care about (e.g. sin 360.0° = 0, NOT an exception)

The only reason why you should ever need to catch exceptions is to print error messages, or to protect your program from crashing when doing something dangerous (e.g. calling a external library), especially in Lua, where errors are generally strings not objects.

That said, pcall is powerful enough for what most people need (i.e. printing error messages), and for those who need more, for whatever reason that may be, there are several ways to achieve identical results as try-catch-finally statements.




--
Alysson Cunha / AlyssonRPG
http://www.rrpg.com.br - Jogue o tradicional RPG de mesa online
http://www.alyssoncunha.com.br - Me conheça melhor