OMG and OMGWTF are the keywords for switch; I think it would be best to have something more different for exceptions.
At some point, PLZ TRY was considered for marking a "try" block, OH NOES was considered for marking an "exception" block, and ANYWAYZ was considered for marking a "finally" block. I think that syntax is very expressive and stylistically consistent as well as distinct and memorable.
- Code: Select all
BTW It would look like this:
VISIBLE "IM DIVIDIN"
QUOSHUNT OF 10 AN 0
VISIBLE "UR NOT CHUCK NORRIS U CANT DIVIDE BY 0"
VISIBLE "IM DONE DIVIDIN"
But in a larger sense, I don't think that LOLCODE has a very well-defined system for exceptions or try-throw-catch in general. At the very least, I haven't been able to observe any documentation to that effect. Before we start trying to try-throw-catch syntax in LOLCODE, it's important to figure out how we want try-throw-catch to work in the first place. Do we want the user to be able to throw arbitrary values, of any type? Should there be a canonical exception type? It's common in many languages that allow try-throw-catch to catch multiple types of exception; should we specify which exception we're catching in the catch statement, or should have one catch statement and then the user can do logic inside to figure out what action to take?
Here's what I like: I like to be able to throw whatever I want and be able to catch whatever I want by listing an exception type in the catch statement. I think that LOLCODE should use IT to handle exceptions; the behavior should be consistent with implicitly returning IT if a function doesn't return anything explicitly.
Here's what I would propose:
PLZ TRY marks a try block.
OH NOES marks a catch block. To check IT for a particular value, you can use OH NOES, I C <expression>
ANYWAYZ marks a finally block.
O NO is syntax for "throw."
PLZ TRY must be followed by at least one OH NOES or ANYWAYZ block.
O NO can be followed by an expression: the value of this expression is stored in IT. If O NO is not followed by an expression, then IT well have whatever value it has anyway.
To catch multiple different exceptions, check the value in IT.
It would look something like this:
- Code: Select all
I HAS A result ITZ SUM OF num1 AN num2
IF BOTH SAEM result AN 0
OH NO "WHY YR NUMBRS SUM 2 ZERO?!"
IF BOTH SAEM result AN BIGGR OF num1 AN num2
OH NO "WHY YOU INPUT ZERO?!"
VISIBLE SMOOSH result AN " IS THE SUM OF YR NUMBRS" MKAY
OH NOES, I C "WHY YOU NUMBR SUM 2 ZERO?!"
OH NOES, I C "WHY YOU INPUT ZERO?!"
VISIBLE "I IS DONE WITH RITHMETIC"
This is a bit awkward if you can't explicitly do type comparison, which is its own discussion. If you could check the type of the value that was thrown, then it would probably make the most sense to check the type of exception, rather than an explicit value. It should be possible to do either, as long as I C performs an arbitrary comparison to IT.