Re: [xsl] catching Java exceptions in saxon?

Subject: Re: [xsl] catching Java exceptions in saxon?
From: Elliotte Rusty Harold <elharo@xxxxxxxxxxxxxxx>
Date: Thu, 18 Apr 2002 06:43:31 -0400
I wrote about this recently in Chapter 17 of Processing XML with Java. See http://www.cafeconleche.org/books/xmljava/chapters/ch17s03.html#d0e27152

In brief:

If the extension function throws an exception, as calculate() might if it's passed a negative number as an argument, then the XSLT processing will halt. XSLT has no way to catch and respond to exceptions thrown by extension functions. Consequently, if you want to handle them, you'll need to handle them in the Java code. After catching the exception, you'll want to return something. Possibilities include:

* A String containing an error message
* A NodeList containing a fault document
* An integer error code

Since this may not be the same type you normally return, you'll probably need to declare that the method returns Object to give you the additional flexibility. For example, this method returns an error message inside a String instead of throwing an exception:

public static Object calculate(int n) {

if (n <= 0) {
return
"Fibonacci numbers are only defined for positive integers";
}
BigInteger low = BigInteger.ONE;
BigInteger high = BigInteger.ONE;
for (int i = 3; i <= n; i++) {
BigInteger temp = high;
high = high.add(low);
low = temp;
}
return high;


}

This method returns -1 (an illegal value for a Fibonacci number) instead of throwing an exception:

public static BigInteger calculate(int n) {

if (n <= 0) return new BigInteger("-1");
BigInteger low = BigInteger.ONE;
BigInteger high = BigInteger.ONE;
for (int i = 3; i <= n; i++) {
BigInteger temp = high;
high = high.add(low);
low = temp;
}
return high;


}

It would be up to the stylesheet to check for the error code before using the result, and handle such a situation appropriately. In this example, that might require calling the extension function before any output is generated, storing the result in a variable, and deciding whether to output a successful response or a fault document based on the value of that variable. Waiting until the template for the int element is activated would be too late because by that point substantial parts of a successful response document have already been generated.
--


+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo@xxxxxxxxxxxxxxx | Writer/Programmer |
+-----------------------+------------------------+-------------------+
|          The XML Bible, 2nd Edition (Hungry Minds, 2001)           |
|             http://www.cafeconleche.org/books/bible2/              |
|   http://www.amazon.com/exec/obidos/ISBN=0764547607/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://www.cafeaulait.org/      |
|  Read Cafe con Leche for XML News: http://www.cafeconleche.org/    |
+----------------------------------+---------------------------------+

XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Current Thread