• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method arguments and values

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a situation where I need to capture method arguments and its values whenever an exception is generated. I checked the Exception and Throwable classes but they don't hold any information about the method parameters.

Is there any way to acheieve this?

TIA
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I seem to remember seeing this asked before and I think the answer was no. Try searching thru the three Java In General forums and you might find the post I'm thinking about.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can pass a String to the constructor of any Throwable, so you can put details of the parameters in that.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the specific subclass of Throwable stored the arguments in its fields, you can introspect (using Reflection) the Throwable object to see what fields and methods it has that might represent those arguments. That's if they're not private, of course.

The JavaBeans API might help in finding JavaBeans-compatible getter methods.

If the Throwable object has not stored the arguments anywhere, or encoded them into the message string, there is no way to retrieve them.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking of something like this:-More tedious, but simple and you don't have to mess about with reflection. I think I have got the two arguments the right way round. When you catch the new Exception you can get the original Exception from it and get its stack trace to find where it occurred.
 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all of you for taking stab at this.

My problem is some what tricky, I have to log details (including parameter passed) for every exceptions generated by the application. I was thinking of minimizing the changes in order to set this up, but it seems there is no easy way other then making wholesome changes in order to capture method parameters.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, one approach would be to use something like AspectJ at compile time, to insert bytecode at every exception constructor to take the string message argument and append a listing of the current method parameters just before the constructor is invoked. It would take some time to learn how to use AspectJ for this, but once you've done that it should require no modification of the rest of the code.
 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Yim for your reply,

I am familiar with AspectJ, its just I was searching for a simpler solution if there exist any.
reply
    Bookmark Topic Watch Topic
  • New Topic