Exception handling is a mechanism used to prevent checking status values after every function call. This mechanism is used to remove business logic from error handling code.
PLZ
<statement-list>
O NOES <Expression>
<Exception handling statement-list>
O NOES <Expression>
<Exception handling statement-list>
...
O WEL
<Always executed block of code>
KTHX
PLZ starts the attempted block of code. This code executes until an exception is thrown, or it reaches the O NOES block, whichever comes first.
The O NOES keyword is used to evaluate and catch raised exceptions. If an exception is raised in the PLZ block, and it meets the O NOES criteria then the O NOES block is executed. An exception only reaches one and only one O NOES block.
The O NOES keyword is followed by an expression. If the expression evaluates to true using a thrown exception then all code between the RTFM keyword and the next O NOES/O WEL/KTHX keywords is run. The expression operates on the thrown exception using the IT variable. The block of exception handling code is run with the Exception in the IT value. (Evaluating the RTFM expression does not set the IT value).
To catch any exception thrown, simply make the expression always evaluate to true.
PLZ
<Business logic Block>
O NOES WIN
<Exception Handling Block>
KTHX
O NOES blocks are used to handle error conditions and recover the program to a manageable state.
O WEL denotes a block of code that will always be executed regardless of exceptions being thrown. This block is closed by a KTHX.
The O WEL block is commonly used to close open resources, such as files, database connections, sockets, etc.
The DO NOT WANT keyword is used to “raise” an exception. When an exception is raised, all O NOES expressions are evaluated in order for each containing PLZ/KTHX block, and working out of containing blocks. As stated above, an exception reaches one and only one O NOES block.
PLZ
PLZ
I HAS A var ITZ "No Gud"
DO NOT WANT var
O NOES BOTH SAEM IT AN "Bad Kitteh"
BTW Not caught in this block
KTHX
O NOES BOTH SAEM IT AN "No Gud"
VISIBLE "Itz no gud" BTW This is run
O NOES WIN
VISIBLE "Iz got all ur bases" BTW This is NOT run
KTHX
The scope of variables is unchanged by the exception handling blocks. If a variable is declared inside a PLZ block, it is available inside the O NOES block and the AWSUM THX block as long as the variable was declared before an exception was thrown.
An example block is shown below
PLZ
I HAS A filezorz ITZ OPEN 'TestFile.csv'
I HAS A line
IM IN YR filez WILE filezorz!!moar
line R filezorz!!gimmeh
BTW Process a line
IM OUTTA YR filez
O NOES BOTH SAEM IT!!name AN "BadFilezorz"
BTW Catch exceptions with name "BadFilezorz"
INVISIBLE "FilezNaem R no gud"
O WEL
BTW Make sure the file gets closed regardless.
filezorz!!close
KTHX