/** * Check whether the given exception is compatible with the specified * exception types, as declared in a throws clause. * @param ex the exception to check * @param declaredExceptions the exception types declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, @Nullable Class<?>... declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { for (Class<?> declaredException : declaredExceptions) { if (declaredException.isInstance(ex)) { return true; } } } return false; }
/** * Check whether the given exception is compatible with the specified * exception types, as declared in a throws clause. * @param ex the exception to check * @param declaredExceptions the exception types declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, @Nullable Class<?>... declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { for (Class<?> declaredException : declaredExceptions) { if (declaredException.isInstance(ex)) { return true; } } } return false; }
@Test public void isCheckedException() { assertTrue(ObjectUtils.isCheckedException(new Exception())); assertTrue(ObjectUtils.isCheckedException(new SQLException())); assertFalse(ObjectUtils.isCheckedException(new RuntimeException())); assertFalse(ObjectUtils.isCheckedException(new IllegalArgumentException(""))); // Any Throwable other than RuntimeException and Error // has to be considered checked according to the JLS. assertTrue(ObjectUtils.isCheckedException(new Throwable())); }
/** * Check whether the given exception is compatible with the exceptions * declared in a throws clause. * @param ex the exception to checked * @param declaredExceptions the exceptions declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { int i = 0; while (i < declaredExceptions.length) { if (declaredExceptions[i].isAssignableFrom(ex.getClass())) { return true; } i++; } } return false; }
/** * Check whether the given exception is compatible with the exceptions * declared in a throws clause. * @param ex the exception to checked * @param declaredExceptions the exceptions declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { int i = 0; while (i < declaredExceptions.length) { if (declaredExceptions[i].isAssignableFrom(ex.getClass())) { return true; } i++; } } return false; }
/** * Check whether the given exception is compatible with the specified * exception types, as declared in a throws clause. * @param ex the exception to check * @param declaredExceptions the exception types declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, @Nullable Class<?>... declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { for (Class<?> declaredException : declaredExceptions) { if (declaredException.isInstance(ex)) { return true; } } } return false; }
/** * Check whether the given exception is compatible with the specified * exception types, as declared in a throws clause. * @param ex the exception to check * @param declaredExceptions the exception types declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, @Nullable Class<?>... declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { for (Class<?> declaredException : declaredExceptions) { if (declaredException.isInstance(ex)) { return true; } } } return false; }
/** * Check whether the given exception is compatible with the exceptions * declared in a throws clause. * @param ex the exception to checked * @param declaredExceptions the exceptions declared in the throws clause * @return whether the given exception is compatible */ public static boolean isCompatibleWithThrowsClause(Throwable ex, Class<?>[] declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { int i = 0; while (i < declaredExceptions.length) { if (declaredExceptions[i].isAssignableFrom(ex.getClass())) { return true; } i++; } } return false; }