Hi,
I am currently experimenting with java code loaded as a basex module.
There I throw custom exceptions, which I want to catch and handle in XQuery.
Unfortunately, the information about the thrown Java exception seems to get lost.
What options do I have to throw my own error messages from my java module that do not immediately become "XPTY0004"?
Many thanks and best regards
Andreas
Hi Andreas,
Exceptions in Java bindings are all caught by BaseX as we want to keep control over unexpected behavior. For custom exceptions, the recommendable approach is to throw instances of the QueryException class. This way, you can directly access the error description:
// JAVA import org.basex.query.*;
public class YourClass { public static void yourMethod() throws QueryException { throw new QueryException("Stopped..."); } }
(: XQUERY :) try { Q{java:YourClass}yourMethod() } catch basex:error { (: result: "Stopped..." :) $err:description }
You can also define a custom error code as follows:
// JAVA import org.basex.query.*;
... throw new QueryException( null, new QNm("custom"), "Stopped..." );
(: XQUERY :) try { ... } catch custom { ... }
Hope this helps, Christian
Hi Christian,
it works like a charme.
Thanks a lot.
Andreas
Von: Christian Grün christian.gruen@gmail.com Gesendet: Montag, 29. August 2022 13:58 An: Andreas Hengsbach | nexoma andreas.hengsbach@nexoma.de Cc: BaseX basex-talk@mailman.uni-konstanz.de Betreff: Re: [basex-talk] Exception handling in XQuery-Java-Binding
Hi Andreas,
Exceptions in Java bindings are all caught by BaseX as we want to keep control over unexpected behavior. For custom exceptions, the recommendable approach is to throw instances of the QueryException class. This way, you can directly access the error description:
// JAVA
import org.basex.query.*;
public class YourClass {
public static void yourMethod() throws QueryException {
throw new QueryException("Stopped...");
}
}
(: XQUERY :)
try {
Q{java:YourClass}yourMethod()
} catch basex:error {
(: result: "Stopped..." :)
$err:description
}
You can also define a custom error code as follows:
// JAVA
import org.basex.query.*;
... throw new QueryException(
null, new QNm("custom"), "Stopped..."
);
(: XQUERY :)
try { ... } catch custom { ... }
Hope this helps,
Christian
basex-talk@mailman.uni-konstanz.de