• 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

UserDefinedException handling

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I have a couple of user defined exception classes and I want to have a handler for all of those exception classes that I write. For example, in my business logic, I catch one of those user defined exceptions and in the catch block, I instantiate my handler which takes the reference for the exception as the argument along with a String message...sothing like below,



And in the handler class, I need to throw a new object of type e, something like below,

The Handler constructor,



My goal is to say

throw new UserException(); in the Handler constructor and this UserException() part will be dynamic. Any suggestions on how to implement it?

Thanks in advance!
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the question what to write in the UserException()? Dynamic handling of exception? Could you explain how you would want to handle it?

The code written looks fine.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your goal to "throw new UserException()"
Since you've got the Class object for the class e.getClass() can you use reflection to create a new instance of UserException and then throw that?

Not sure why you'd want to do that instead of just doing "new UserException()"
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Norm Radder:
your goal to "throw new UserException()"
Since you've got the Class object for the class e.getClass() can you use reflection to create a new instance of UserException and then throw that?

Not sure why you'd want to do that instead of just doing "new UserException()"



It may be of any type...today it is UserException(), tomorrow it is UserException1() and so on...so I generically want to throw the Object that comes in. I tried e.getClass() and then how can I create a new object using reflection? Can you please let me know!
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you follow Factory Pattern.....
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You Can also do some thing like...

ex.getClass().newInstance(); //Don't forget to have default constructor


Instead,Better follow factory pattern

or You can observe JAVA API classes with getInstance() method.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:

throw new UserException(); in the Handler constructor and this UserException() part will be dynamic. Any suggestions on how to implement it?



Have a look at Factory Pattern or Abstract Factory Pattern which is perfectly eligible candidate for your case!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic