If you can't successfully force quit, the solution is to hit the interrupt key (or programmer's key combo, depending on the device), and do:
SM FA700 A9F4
PC FA700
G
First line sets the memory at address FA700 (usually a safe range to overwrite) to A9F4 ("exit to shell" trap).
Second line sets the program counter to that address (telling the computer to process from that memory address next)
Third line runs from the current address, causing the OS to trigger an "exit to shell" event, which SHOULD drop you back into the Finder.
At that point, you want to Special -> Shut Down, as things will be almost guaranteed to be unstable and crash-prone. Gives you the option to cleanly shut down instead of just rebooting though.
Since you're writing your own code, you're probably dropping in and out of the debugger. You can just use that same minibug routine but set the memory at whatever address is safe in your existing stack to A9F4 and run it. Or, you can pre-set an address in your own stack to A9F4, and have a debug handler that points to that address if you get into a nested loop situation beyond a specified depth.