주로 command pattern에서 특정 클래스의 메소드를 사용하기 위해서 invoke 함수를 사용하는 경우가 있다.
그런데 invoke를 사용했을 때 Exception이 발생하게 되면 InvocationTargetException을 얻게 되고,
내부의 실제 문제가 되는 Exception을 구할 수 없는 경우가 있다.
이럴 때는 getCause();를 이용해 내부의 실제 문제가 되는 Exception을 얻을 수 있다.
이거 하나를 찾음으로써 two man/a month는 절약한듯.. 기쁘다.
아래는 소스.
Method handler = null;
String methodName = null;
Class[] clsParams = null;
Object[] objParams = null;
…
try{
….
handler = this.getClass().getDeclaredMethod( methodName, clsParams );
handler.invoke( this, objParams );
}catch(Exception ex){
Exception inner_ex = new Exception();
if (ex.getCause() instanceof Exception) {
inner_ex = (Exception) ex.getCause();
throw inner_ex;
}
else{
throw ex;
}
}