instanceof判断一个实例是不是一个类的实例
private static final Throwable Exception = null;
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub HibernateException he = new HibernateException(Exception); NestableRuntimeException nre = new NestableRuntimeException(); NestableException ne = new NestableException(); if (nre instanceof Exception) { System.out.println("NestableRuntimeException is Exception"); } if (nre instanceof RuntimeException) { System.out.println("NestableRuntimeException is RuntimeException"); } if (he instanceof RuntimeException) { System.out.println("HibernateException is RuntimeException"); } if (he instanceof Exception) { System.out.println("HibernateException is Exception"); } if (ne instanceof Exception) { System.out.println("NestableException is Exception"); } }
结果
NestableRuntimeException is ExceptionNestableRuntimeException is RuntimeExceptionHibernateException is RuntimeExceptionHibernateException is ExceptionNestableException is Exception