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

Refactoring with Reflect API

Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
Ok right now I have several methods that follow the next repetition.

Search throughout a list use a method to see if an object is the one required and return an unmodifiable value of the method if it isn't throw an exception.

Example:


What I'm looking is through a method encapsulate this common repetition.

SortOfCode:


Unfortunately, I can't throw the exception through the throwable.

How can I pull it off?


regards,
Victor M. Pereira
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
If you want to throw a Throwable then you need to catch a Throwable. Exception is a subclass of Throwable so simply catching or throwing an Exception won't do.
You need to change line 1 toor line 10 to


Joanne
Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
Well the catch on line 10 is for trapping all exceptions that come of java.lang.reflection API. While the exception thrown by the methods that are invoked are the ones I need to throw.
Well I think I have spotted a small mistake on my code example. I'll try it and report on it.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
But your testSomething method is declared to have a Throwable parameter and you try to throw (on line 9) whatever object that parameter refers to. It doesn't matter if, in your code, the actual object you pass in is only an Exception or one of its subclasses, you have said it is possible for that object to be a Throwable, so when you throw it your catch code has to handle Throwables or your method needs to be declared to throw a Throwable.
 
wood burning stoves
 
subject: Refactoring with Reflect API