• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Annotations and exception handling

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder if anyone can help.
I'm trying to write a struts 2 component and as part of that I am trying to use XWork's DI (I've decided to post here as I think its more to do with Java annotations rather than struts or xworks)
I want to use the struts2 / xworks Inject annotation to get a value from the struts properties files into my class. Esssentially this is what I want to do:



Where 'fred' in the example is the name of a property in the properties file. Obviously 'fred' is a value I've used to test what happens when the name/value pair is not found in the properties file. In this case the xworks class throws an exception.
This is fine, except I cant work out how to catch the exception! I am not familiar with java5 annotations (I cut my teeth on the 1.4 stream ) - so if anyone could point me in the right direction I'd very much appreciate it.

Thanks

Nathan
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The annotation doesn't do anything itself. I'm not familiar with the tool you're using, but someplace, there is code that will look for these annotations and try to instantiate your class. It's that code that will throw the exception. In these kinds of systems, that code tends to be buried pretty far down in the guts, and I'm not sure there's any easy way to catch an exception like this and continue. A missing property would be considered a fatal configuration error.

If this is the annotation you're talking about, looks like there's a "required" argument which defaults to "true" -- I imagine toggling that lets you prevent the exception altogether.
 
Nathan Russell
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest - yes, that was the annotation I was after, and by setting the required arguement to false it does not throw the exception, which means I can handle it as I need to
Thank you very much for your help with this, very much appreciated;

Cheers

Nathan
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Moving to Struts. XWork is being folded into Struts, and the version of Guice it uses is a very early fork, so... it's pretty much Struts.)
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic