InvocationTargetException und InvocationHandler.

Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Hallo,

ich habe/hatte in einem InvocationHandler den folgenden Code in der Invoke-Methode.
Code:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
  return method.invoke(target, args);
}
Also ein ziemlich triviales Delegieren des Aufrufs an das Zielobjekt 'target'.
Jetzt angenommen, ich werfe in einer der Methoden des Zielobjektes (target) die IllegalArgumentException.
Laut der API ist eine solche Exception OK, bzw. muß nicht benannt werden, da es eine RuntimeException
ist.
Throws:
Throwable - the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.
(Aus der Beschreibung zu InvocationHandler.invoke(...))

Trotzdem kommt dies beim Aufrufer als UndeclaredThrowableException an, da die IllegalArgumentException
zusätzlich noch in InvocationTargetException 'verpackt' wird.
Es steht in der API aber nichts davon, dass ALLE Exceptions grundsätzlich als InvocationTargetException geworfen
werden.

Ich habe es jetzt wie folgt gemacht,
Code:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
  try
  {
    return method.invoke(target, args);
  }
  catch(InvocationTargetException e)
  {
    // Da alle Exceptions der obigen Invoke-Methode in eine InvocationTargetException
    // 'verpackt' werden, wird an dieser Stelle die Exception geworfen, die das 
    // Scheitern des Aufrufs verursacht hat.
    throw e.getTargetException();
  }
}
bin mir aber nicht sicher, ob es Fälle geben kann, wo die Target-Exception
(siehe e.getTargetException()) von InvocationTargetException 'null' sein kann?
Weiß es vielleicht jemand?

Gruß,
Michael
 
B

Beni

Gast
Dann mach doch eine Abfrage, "f`if `( e.getTargetException == nulll... ", und im else-Teil verschickst du sonst eine Exception.
 
G

Guest

Gast
OK, egal, was ich da für Exceptions in 'target' auslöse, es wird immer eine
InvocationTargetException erzeugt.
Klartext: Wenn InvocationTargetException, dann auch mit 'cause' bzw.
TargetException. :roll:
Ich denke, ich kann's so stehen lassen, ohne eine Überraschung zu erleben.
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben