Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UserDefinedException handling

 
Joe Harry
Ranch Hand
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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!
 
Joe Matthew
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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.
 
Norm Radder
Ranch Hand
Posts: 887
6
  • Mark post as helpful
  • send pies
  • 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 Harry
Ranch Hand
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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!
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you follow Factory Pattern.....
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • 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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic