zoukankan      html  css  js  c++  java
  • 关于java.lang.reflect.InvocationTargetException

      今天遇到java.lang.reflect.InvocationTargetException错误,卡了好一会儿,报错代码

      

    try { 
                Class<?> c= Class.forName("com.csdhsm.chat.server.handler." + handlerName); 
                Class<?>[] arg=new Class[]{SocketRequest.class,SocketResponse.class}; 
                Method method=c.getMethod(action,arg);
                Logger.getLogger(Logger.class).info("实例化处理器" + handlerName);
                method.invoke(c.newInstance(), new Object[]{request,response}); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 

      错误锁定在 method.invoke(c.newInstance(), new Object[]{request,response}); 这句话,我一直是以为反射出了什么错,后来google之后才发现是Method 这个方法里面出了错误,如果想要看到里面的错误,需要这样做  

    try {
                Method method = obj.getClass().getMethod(methodName);
                method.invoke(obj);
            } catch(NoSuchMethodException e) {
                throw new RequestNotFoundException(
                        "无法找到方法:" + methodName, e);
            } catch(InvocationTargetException e) {
                Throwable t = e.getCause();
                t.printStackTrace();
                throw new ObjectFactoryException(t);
            } catch(Exception e) {
                e.printStackTrace();
                throw new ObjectFactoryException(e);
            }
    重点在Throwable t = e.getCause(); 这句话。

    参考链接 http://www.oschina.net/question/86510_14830
  • 相关阅读:
    Apache 浏览器访问限制配置
    Apache 防盗链配置
    Apache 静态缓存配置
    Apache 日志管理
    Apache 域名跳转配置
    搭建完全分布式的hadoop[转]
    Laravel Cheat 表 http://cheats.jesse-obrien.ca/#
    spring-data-mongodb必须了解的操作
    Java MongoDB 资料集合
    MongoDB分片技术[转]
  • 原文地址:https://www.cnblogs.com/a294098789/p/5495007.html
Copyright © 2011-2022 走看看