Programming languages typically have a try-catch mechanism for dealing with exceptions. The try
block contains the code you want to execute and the catch
block contains the code that will be executed if an error occurs in the try
block.
Here is an example of a simple try-catch block in Java, which attempts to parse a string into an int and returns -1 if there is an error.
try { return Integer.parseInt(x); } catch (NumberFormatException e) { e.printStackTrace(); return -1; }
In this post, I will describe the try-catch equivalent for exception handling in the q programming language.
.Q.trp[f;x;g] - for unary functions
For unary functions, you can use .Q.trp
(Extend Trap), which takes three arguments:
f
- a unary function to executex
- the argument off
g
- a function to execute iff
fails. This function is called with two arguments, the error stringx
and the backtrace objecty
For example:
// Define a function which casts a string to int parseInt:{[x] "I"$x} // Define an error function which prints the stack trace and returns -1 // Note: .Q.sbt formats the backtrace object and 2@ prints to stderr g:{[x;y] 2@"Error: ",x,"\nBacktrace:\n",.Q.sbt y;-1i} // Try calling the function (wrapped by .Q.trp) with a valid argument .Q.trp[parseInt;"123";g] 123i // Try calling the function (wrapped by .Q.trp) with an invalid argument // The error function is called and the stack trace is printed .Q.trp[parseInt;`hello;g] Error: type Backtrace: [2] parseInt:{[x] "I"$x} ^ [1] (.Q.trp) [0] .Q.trp[parseInt;`hello;g] ^ -1i
Note: An alternative is to use Trap At which has syntax @[f;x;e]
but you won't get the backtrace, so it's better to use .Q.trp
.
.[f;args;e] - for n-ary functions
.Q.trp
only works for unary functions. For functions with more than one argument, you need to use Trap which has the syntax .[f;args;e]
. The error function e
does not take any arguments, which means no backtrace available. For example:
// Define a ternary function that sums its arguments add:{[x;y;z] x+y+z} .[add;1 2 3;{2@"Failed to perform add";-1}] 6 .[add;(1;2;`foo);{2@"Failed to perform add\n";-1}] Failed to perform add -1