关于java.lang.reflect.InvocationTargetException
2016-05-15 12:27
381 查看
今天遇到java.lang.reflect.InvocationTargetException错误,卡了好一会儿,报错代码
错误锁定在 method.invoke(c.newInstance(), new Object[]{request,response}); 这句话,我一直是以为反射出了什么错,后来google之后才发现是Method 这个方法里面出了错误,如果想要看到里面的错误,需要这样做
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[/code]
相关文章推荐
- eclipse到Android Studio的项目迁移
- 从最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate
- JAVA连接MYSQL测试
- Maven基本配置
- java 生成xml文件
- Java字符串的比较是否相同
- eclipse代码自动补全的设置
- SpringMVC项目中中文字符乱码问题及解决办法总结(非专业最优解决办法) -- ajax传值乱码; request.getParameter()乱码;
- Java片段 Java中Double和Long互相转换
- java 线程池执行 callable 接口,future 接收callable 接口的返回值
- Spring实战-使用表达式装配(三)
- jdk7新特性一
- Spring Boot 静态资源处理
- java设计模式:迭代器设计模式
- java多线程
- Struts2 文件下载
- Java 基础知识(四)之基本类型与运算
- Polymorphism(多态)
- Java中的自定义注解
- JVM学习(一)——java技术体系