4 ms·
This looks very nice. From what I've seen, the only thing I'd change is the way it deals with exceptions. Specifically, I'm not very happy with this way of thr
by Sandman 15y ago
This looks very nice. From what I've seen, the only thing I'd change is the way it deals with exceptions.
Specifically, I'm not very happy with this way of throwing
exceptions (example taken from Gosu documentation on exceptions [1]):
throw "x does not equal zero"
and then later chatching them like this :
catch( e ) {
if( e == "x equals zero" ) { return( e + " handled
locally." ) }
I want to be able to specify the exact type of the exception, and catch them based on their type, instead of having to throw a generic error and then check using "if" statements whether it's expression is equal to a particular String or not. If I understood correctly, this is what Gosu is actually doing, right?
[1] http://gosu-lang.org/doc/wwhelp/wwhimpl/js/html/wwhelp.htm#href=Gosu%20Reference%20Guide/exceptionhandling.10.3.html http://gosu-lang.org/doc/wwhelp/wwhimpl/js/html/wwhelp.htm#h...
- tikhonj 15y agoIf I remember correctly, you can catch exceptions by type; you just don't have to. The code looks like this: try { // Maybe throws IOException } catch (e : IOException) { // Handle it } I think this does exactly what you want. There's more documentation here[1] (the documentation format is a little annoying, unfortunately). [1]: http://gosu-lang.org/doc/wwhelp/wwhimpl/js/html/wwhelp.htm#href=Gosu%20Reference%20Guide/exceptionhandling.10.3.html http://gosu-lang.org/doc/wwhelp/wwhimpl/js/html/wwhelp.htm#h...
- Sandman 15y agoWell, this is not exactly what I want. You can catch exceptions by type, but only from some code (for example, a Java library) that actually throws different types of exceptions. In Gosu, I can't throw them by type, at least from what I've seen. I can only throw some generic type of exception (or is it an error? Or something else that implements Throwable? Who knows, Gosu doesn't tell you anything about that) that contains some expression (a String literal), and then anybody who wants to catch it, needs to know that exact expression in order to catch the exception. The more I think about it, the more terrible this looks. I can't even specify to the users of my code which exceptions they should look after (because, it seems, Gosu doesn't allow the throwing of checked exceptions and the 'throws' clause). So if I write some kind of library in Gosu, and if that library is throwing exceptions, I'm basically forcing the users of my library to catch this generic exception (or whatever it is), then put a breakpoint in the catch block and every time something gets caught inspect the expression, hoping that if they test long enough they'll find all of my expressions. Then, they need to write code that covers all the possible cases (a bunch of "if" statements). I sincerely hope that I somehow completely misunderstood what Gosu is trying to do here, because otherwise this is just a terrible solution. That would be a real shame though, because, other than this, I honestly like the language.
- tikhonj 15y agoI think you can throw anything, including whatever sort of exception you could want. However, the real idea is that you should not use exceptions for anything expected like IO issues. Exceptions are meant to signify errors that could not be handled--unlike in Java, you shouldn't use exceptions for control flow. The real issue is one of semantics--in Java, one concept (exceptions) is used to represent two do disparate things: signal unexpected errors (runtime exceptions) and manage expected conditions (checked exceptions). A lot of people do not like checked exceptions and think exceptions should only really be used for the former case. Thus the lack of checked exceptions is a feature rather than a bug; moreover, it is a feature other people in this thread have already requested. So, the potentially annoying answer is that people using your library should not expect particular exceptions--that is not what they should be used for in Gosu. Coincidentally, this is very similar to how JavaScript deals with exceptions, and I have had no issues there at all.