[xwiki-users] Handling exceptions from components
What's the preferred way to deal with errors occuring in Java components? Is it OK to throw an exception from within the component, and can this be caught within the Velocity or Groovy code that calls it?
On 28 Nov 2014 at 01:00:37, Bryn Jeffries ([email protected](mailto:[email protected])) wrote:
What's the preferred way to deal with errors occuring in Java components? Is it OK to throw an exception from within the component,
yes
and can this be caught within the Velocity or Groovy code that calls it?
errors are automatically caught in the script macros ({{velocity}}, {{groovy}}, etc). Thanks -Vincent
Said Vincent:
and can this be caught within the Velocity or Groovy code that calls it?
errors are automatically caught in the script macros ({{velocity}}, {{groovy}}, etc).
OK, looks like Groovy has explicit try/catch (http://groovy.codehaus.org/JN3035-Exceptions). I couldn't see anything about Velocity catching exceptions (only about Velocity throwing exceptions - http://velocity.apache.org/engine/devel/developer-guide.html#exceptions). So if I have a component that I call from velocity with $mycomponent.foo() and the method throws an exception (IOException, for instance), can I control what the Velocity script does about this?
On Fri, Nov 28, 2014 at 9:17 PM, Bryn Jeffries <[email protected]> wrote:
Said Vincent:
and can this be caught within the Velocity or Groovy code that calls it?
errors are automatically caught in the script macros ({{velocity}}, {{groovy}}, etc).
OK, looks like Groovy has explicit try/catch (http://groovy.codehaus.org/JN3035-Exceptions). I couldn't see anything about Velocity catching exceptions (only about Velocity throwing exceptions - http://velocity.apache.org/engine/devel/developer-guide.html#exceptions).
Yes there is nothing in standard Velocity but XWiki add some tools to Velocity and among other things some try/catching support, see http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HVeloci.... What Vincent meant by automatically caught is that you get to see the error in the error where you have put the {{velocity}} macro, it does not break the whole page.
So if I have a component that I call from velocity with $mycomponent.foo() and the method throws an exception (IOException, for instance), can I control what the Velocity script does about this?
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Thomas Mortagne
I misunderstood what you wanted. I thought you wanted to know what the {{velocity}} or {{groovy}} macros would display. They automatically catch exceptions. Now it seems your question is different and you wish to be able to catch exceptions in your own script. XWiki offers a scripting API for all Java API (for example when you call $services.xxx). It’s not the same API as the Java API. And our rule is in general to not throw Exceptions but instead to return null when there are problems. The main reason was because of Velocity indeed. That said, we’ve recently introduced a way to catch exceptions in Velocity: http://www.xwiki.org/xwiki/bin/view/ReleaseNotes/ReleaseNotesXWiki63#HAbilit... Hope it helps -Vincent On 28 Nov 2014 at 21:18:56, Bryn Jeffries ([email protected](mailto:[email protected])) wrote:
Said Vincent:
and can this be caught within the Velocity or Groovy code that calls it?
errors are automatically caught in the script macros ({{velocity}}, {{groovy}}, etc).
OK, looks like Groovy has explicit try/catch (http://groovy.codehaus.org/JN3035-Exceptions). I couldn't see anything about Velocity catching exceptions (only about Velocity throwing exceptions - http://velocity.apache.org/engine/devel/developer-guide.html#exceptions).
So if I have a component that I call from velocity with $mycomponent.foo() and the method throws an exception (IOException, for instance), can I control what the Velocity script does about this?
participants (3)
-
Bryn Jeffries -
Thomas Mortagne -
vincent@massol.net