未捕获自定义RuntimeException

Gat*_*het 3 java exception-handling java-ee runtimeexception

我有自己的ApplicationException女巫,扩展了RuntimeException:

package com.haslerrail.aura.common.exception;

public class ApplicationException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private final ErrorCode errorCode;

    private final Object[] arguments;

    public ApplicationException(final ErrorCode errorCode, final Object... arguments) {
        super();
        this.errorCode = errorCode;
        this.arguments = arguments;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public Object[] getArguments() {
        return arguments;
    }

}
Run Code Online (Sandbox Code Playgroud)

此异常将由我的无状态bean引发:

public void doSomething()
        throws IOException, ApplicationException, InternalException {
    if (true) {
        throw new ApplicationException(ErrorCode.FILE_TYPE_NOT_SUPPORTED, "test");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想通过我的sessionScoped bean捕获此runtimeException:

public void doXXX(final FileUploadEvent event) throws SystemException {
    try {

        myStatelessbean.doSomething();  // Throw my ApplicationExcetion

    } catch (final ApplicationException e) {
        System.out.println("catch ApplicationException");
    } catch (final Exception e) {
        System.out.println("catch Exception");
    }

}
Run Code Online (Sandbox Code Playgroud)

我的问题是,异常是由Exception捕获的,而永远不会是由我自己的ApplicationException捕获的!我不知道我在做什么错...

e.printStackTrace()给出异常文本:

javax.ejb.EJBException: com.haslerrail.aura.common.exception.ApplicationException
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304)
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:76)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:32)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165)
    at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:173)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:72)
    at XXXX.myStatelessbean.$$$view353.doSomething(Unknown Source)  
Run Code Online (Sandbox Code Playgroud)

stg*_*stg 6

这里的问题是您正在抛出一个RuntimeException而不是某种已检查的异常。

在这种情况下,当 aRuntimeException发生时,EJBContainer 将回滚事务并抛出 a EJBException(也是 a RuntimeException)结果是,您的客户端不会直接看到您的自定义 Exception 而是EJBException由容器抛出的which。

您可以改为抛出一个已检查的异常(只是不要扩展RuntimeException但是Exception),但是您必须在必要时自行回滚事务。另一方面,您可以展开 EJBContainer 抛出的 EJBException 以获取原因,如果这对您来说真的很有趣。

有关 EJB 中的异常处理的更多详细信息,另请参阅此博客文章:http : //palkonyves.blogspot.de/2013/04/exceptions-and-transactions-in-ejb.html


mar*_*ess 5

请检查@ApplicationExceptionEJB规范指出异常的语义,因此带注释的异常将被包装到客户端。本质上,如果引发任何其他未注释的异常,则@ApplicationExceptionejb容器将引发的异常包装到an中,EJBException然后将其引发给客户端。因此,您将永远无法在客户端捕获实际的异常。

//I always set rollback, cause its needless in most circumstances to commit transaction when an exception occurs.
@ApplicationException(rollback = true, inherit=true)
public class MyApplicationException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private final ErrorCode errorCode;

    private final Object[] arguments;

    public MyApplicationException(final ErrorCode errorCode, final Object... arguments) {
        super();
        this.errorCode = errorCode;
        this.arguments = arguments;
    }

    public ErrorCode getErrorCode() {
        return errorCode;
    }

    public Object[] getArguments() {
        return arguments;
    }

}
Run Code Online (Sandbox Code Playgroud)