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

来源:互联网 发布:java泛型运行时被擦除 编辑:程序博客网 时间:2024/05/17 12:03
为什么我的Class.forName()抛出的异常不是ClassNotFoundException,而是RuntimeException?
时间:2007-06-18  www.jubao163.com  来源: 不详


 所属分类:Java J2SE / 基础类
-----------------------------------------


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"); 



----------------------------------------------------------------------


绝对是ClassNotFoundException 


你真的执行过吗 






--------------------------------------------------------


度过ClassNotFoundException 


--------------------------------------------------------


试过了ClassNotFoundException 


--------------------------------------------------------


哎,终于知道原因了!我用的是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; 
    } 
原创粉丝点击