3 ms·
Well, 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 ty
by Sandman 15y ago
Well, 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.