为什么我的Class.forName()抛出的异常不是ClassNotFoundException,而是RuntimeException?

starflash2003 2007-06-07 03:00:32
try {
Class c = Class.forName("aaaaa");
c.newInstance();
} catch (Exception e) {
if (e instanceof ClassNotFoundException)
System.out.println("ClassNotFoundException");
else if (e instanceof RuntimeException)
System.out.println("RuntimeException");
}
...全文
280 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
starflash2003 2007-06-07
  • 打赏
  • 举报
回复
哎,终于知道原因了!我用的是Axis2包,而Axis2重写了类加载器ClassLoader类中的findClass方法,如下:
protected Class findClass(String name) throws ClassNotFoundException {
Class clazz;
try {
clazz = super.findClass(name);
} catch (ClassNotFoundException e) {
byte raw[];
try {
String completeFileName = name;
/**
* Replacing org.apache. -> org/apache/...
*/
completeFileName = completeFileName.replace('.', '/').concat(".class");
raw = getBytes(completeFileName);
if (raw == null) {
throw new ClassNotFoundException("Class Not found : " + name);
}
} catch (Exception ex) {
// TODO: This, or throw new ClassNotFoundException?
throw new RuntimeException(ex);
}
clazz = defineClass(name, raw, 0, raw.length);
}
return clazz;
}
grant999 2007-06-07
  • 打赏
  • 举报
回复
试过了ClassNotFoundException
grant999 2007-06-07
  • 打赏
  • 举报
回复
度过ClassNotFoundException
killme2008 2007-06-07
  • 打赏
  • 举报
回复
绝对是ClassNotFoundException

你真的执行过吗

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧