aspose file tools*
The moose likes Java in General and the fly likes UserDefinedException handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "UserDefinedException handling" Watch "UserDefinedException handling" New topic
Author

UserDefinedException handling

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Matthew
Ranch Hand

Joined: Jun 10, 2008
Posts: 66
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.


Regards,<br />Joe<br /> <br />"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."<br />--Martin Golding
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
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

Joined: Sep 26, 2006
Posts: 9427
    
    2

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!
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
Why don't you follow Factory Pattern.....
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
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

Joined: Apr 20, 2006
Posts: 3344

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!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
 
subject: UserDefinedException handling