Exception Standardization Guidelines

In j2ee applications, I plan to standardize an exception handling strategy. It no longer swallows exceptions and basically converts all checked exceptions to unchecked ones (with the exception of those that actually can ) be restored. The standard will be that the entire Exception will be returned to the container (for example: http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html ). Exceptions, such as SQLException, cannot be recovered, and if this happens deep inside the data layer, I'm going to put it back. Neverthelesssmall routines, such as email notifications, will be wrapped in a try catch to eliminate an exception from the entire application stream. (EG: Why complete the entire process from completion, because the user didn’t receive an email notification that they didn’t need anyway!)

Any advice on this? Once again, I basically throw all the exceptions back into the container, which has its own error page, which then writes it to log4j.

+3
source share
5 answers

, - . -, , . Runtime, , .

, , Runtime Exception . , :

. - , , . , , , . initCause, . :

public static <T extends Throwable> T initCause(T newException, Throwable cause) {
    return (T) newException.initCause(cause);
}

.

RuntimeException Error Throwables, , , , , - :

public static void throwRuntimeExceptionFromThrowable(Throwable e) {
    if (e == null) {
        return;
    } else if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
    } else if (e instanceof Error) {
        throw (Error) e;
    } else {
        throw new RuntimeException(e.getMessage(), e);
    }
}

. , ( ), , . , - :

public Object someMethod() {
    try {
        return getTheObject();
    } catch (Exception e) {
        throwRuntimeExceptionFromThrowable(e);
    }
}

. .

- RuntimeException ( ) . , , .

+2

, . , , . . , , ( ), , , . , , SQLException, . IOException . , , , "". jdbc PreparedStatement . - . , , . , . - , NullPointerExceptions ArrayIndexOutOfBoundsException , . - , , SQLExceptions, , InvalidDeptException, . . , , .

+1

, Fault Barrier - , . : http://www.oracle.com/technology/pub/articles/dev2arch/2006/11/effective-exceptions.html (. ).

, . . JEE5, @Interceptor - " " - , beans/ .

+1

DB Access , Spring . .

0

, SQLException ( )? . SQLTransientException , (, SQLTransientConnectionException). , .

, , . , .

0
source

Source: https://habr.com/ru/post/1707889/


All Articles