F.E.L.T.

Functionally Equivalent Language Translation

THROW

(throw expression)

For languages that have exception / condition handling systems this instruction is how you raise or signal that an "exception" needs to be thrown and some (hopefully) registered TRY / CRASH handler will grab it and deal with it. Please see that page for more information on catching exceptions.

See also TRY / CRASH


Raising Exceptions

The nature of exception handling across languages us so wide and varied that FELT does little more than allow you to raise an exception. It does not check what you put into the body of thw THROW instruction at all. This is definitely one of those times where you will need to use the #F:WHEN instruction to handle any differences.

PHP Exceptions

In PHP code you have to use the Exception class as the base for any exception that you raise so at first you mightbe tempted to code like this:

(throw (new Exception "I blew up here"))

You will get that looks like this:

throw new Exception("I blew up here");

Javascript Exceptions

Fair enough, but running the same code through the Node.js coder produces this:

throw new Exception("I blew up here");

And I can tell you now that is not going to get past any JavaScript parser on the planet! According to the JavaScript documentation:

The exception can be a string, integer, Boolean or an object.

That's somewhat different that in the PHP world and so you can see that FELT really doesn't have much of a chance and therefore stands back and lets you take the responsibility for it using the conditional inclusion helper: #F:WHEN. So, if I wanted to make it portable I would use the #F:WHEN instruction to do it like this:

(defun do-domething-with (x)
  (if (x-is-valid x)
      (return TRUE)
      (progn
    (#f:when PHPCoder
      (throw (new Exception "X is rubbish, fix it!")))
    (#f:when JSCoder
      (throw "X is rubbish, sort it out...")))))

Please note that you need to enclose them in a PROGN instruction because the syntax of IF is such that it only accepts a single instruction on either the consequent or alternate positions.

Here is the PHP code that is rendered from the above FELT source:

function do_domething_with($x) {
  if (x_is_valid($x)) {
    return TRUE;
  }
  else {
    throw new Exception("X is rubbish, fix it!");
  }
}

And the JavaScript back-end produces code that looks like this:

function do_domething_with(x) {
  if (x_is_valid(x)) {
    return true;
  }
  else {
    throw "X is rubbish, sort it out...";
  }
}

Once again we have portable code without too much fuss!