F.E.L.T.

Functionally Equivalent Language Translation

TRY / CRASH

(try
  expression
  (catch
    (Etype-1 handler)
    (Etype-2 handler)
    (Etype-N handler))
  (finally expression))

This is the FELT instruction to wrap an expression body in a try/catch handler. Note that the expressions are not implied blocks and thus you must use PROGN for the expression, finally expression and handler expressions if they are more than one instruction long.

See also THROW


Try / Catch / Finally / Throw

FELT understands that lots of languages have the concept of 'exceptions' and 'catching exceptions' and so it tries to cater for this across languages by providing the TRY/CRASH instruction. CRASH is just a synonym for TRY and is there for those days when typing the word crash instead of try just feels like the right thing to do. Everybody has bad days.

Some languages do not have the finally part available. If you should try to use a finally handler for that coder, it may or may not blow up on you depending upon how it was written. The stock FELT coders will not blow up but will wrap the code in a comment block so at least you can see that it is not being handled as expected should you examine the source code.

Example

Here is a small example of using the TRY/CRASH instruction so that you can see how to use it. All options are shown, multiple exception handlers and a finally clause:

NOTE: Whatever class-names you use for naming exceptions are not portable as they are going to be target dependent. This of course is usual with FELT... it cannot possibly know the name of every exception across every language and so the responsibility is yours to ensure that meaningful code is ultimately rendered.

It is highly recommended that specifics like this be placed into separate FELT source files and hat your build process only pull in what is correct for the relevant target language being rendered to.

(try
 (progn
   (defvar x 0)
   (defvar f (generate-filename x))
   (call-dodgy-file-function f x))
 (catch
     ((FileIOError e)
      (progn
    (emit "File io error " e)
    (handle-file-error)))
   ((DiskSpaceError dse)
    (progn
      (emit "Disk space error " dse)
      (handle-disk-error))))
 (finally
  (progn
    (release-resources)
    (free-disk-handles))))

Here is the rendered PHP code and you can see that, as PHP does not have the 'finally' part within its repertoire that particular code has been commented out so that when you realise that your finally code isn't working, when you examine the source that has been generated you will know why!

/* PHP does not support a "finally" statement. */
/* {release_resources();
free_disk_handles();} */

try {
  $x = 0;
  $f = generate_filename($x);
  call_dodgy_file_function($f, $x);
}
catch (FileIOError $e) {
  echo "File io error ", $e;
  handle_file_error();
}
catch (DiskSpaceError $dse) {
  echo "Disk space error ", $dse;
  handle_disk_error();
}

The JavaScript for the same FELT source is show here, and this time we can see that as JavaScript does have a try / catch / finally triumvirate that it has been placed into the output as live code:

try {
  var x = 0;
  var f = generate_filename(x);
  call_dodgy_file_function(f, x);
}
catch (e) {
  console.log("File io error "+ e);
  handle_file_error();
}
catch (dse) {
  console.log("Disk space error "+ dse);
  handle_disk_error();
}
finally {
  release_resources();
  free_disk_handles();
}