Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refactoring with Reflect API

 
Victor M. Pereira
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic