Today encountered java.lang.reflect.InvocationTargetException error, card for a good while, error code
try {Class <?> c= class.f ORName ("Com.csdhsm.chat.server.handler." + HandlerName); Class <?>[] arg=new class[]{socketrequest., Socketresponse. class }; Method =c.getmethod (Action,arg); Logger.getlogger (Logger. class ). Info ("instanced processor" + HandlerName); Method.invoke (C.newinstance (), new Object[]{request,response}); catch (Exception e) {E.printstack Trace (); }
Error locked in Method.invoke (C.newinstance (), New Object[]{request,response}); This sentence, I always thought reflected what was wrong, and then Google later found that the method is a mistake, if you want to see the error inside, need to do
Try{Method Method=Obj.getclass (). GetMethod (MethodName); Method.invoke (obj); } Catch(nosuchmethodexception e) {Throw NewRequestnotfoundexception ("Method not found:" +MethodName, E); } Catch(InvocationTargetException e) {throwable T=E.getcause (); T.printstacktrace (); Throw Newobjectfactoryexception (t); } Catch(Exception e) {e.printstacktrace (); Throw Newobjectfactoryexception (e); }
emphasis on throwable t = E.getcause (); This sentence.
Reference link http://www.oschina.net/question/86510_14830
About Java.lang.reflect.InvocationTargetException