Java 捕获多个异常并执行相同的操作

Java 捕获多个异常并执行相同的操作,java,spring,jakarta-ee,Java,Spring,Jakarta Ee,我正在编写一个事务管理系统并捕获6个异常,我需要为每个异常回滚事务。现在我在每个块中都做了,有没有一种方法可以中心化,我知道有一种方法是从方法中抛出异常 catch (RollbackException e1) { e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (Heuristic

我正在编写一个事务管理系统并捕获6个异常,我需要为每个异常回滚事务。现在我在每个块中都做了,有没有一种方法可以中心化,我知道有一种方法是从方法中抛出异常

       catch (RollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicMixedException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicRollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SystemException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        } catch (SystemException e) {
        if(log.isErrorEnabled())
        {
            log.error(e);
        }
        try {
            trans.rollback();
        } catch (RollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicMixedException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicRollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SystemException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    } catch (RollbackException e) {
       if(log.isErrorEnabled())
       {
           log.error(e);
       }
        try {
            trans.rollback();
        } catch (RollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicMixedException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicRollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SystemException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    } catch (HeuristicMixedException e) {
        if(log.isErrorEnabled())
        {
            log.error(e);
        }
        try {
            trans.rollback();
        } catch (RollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicMixedException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicRollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SystemException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    } catch (HeuristicRollbackException e) {
        if(log.isErrorEnabled())
        {
            log.error(e);
        }
        try {
            trans.rollback();
        } catch (RollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicMixedException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (HeuristicRollbackException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SystemException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

如果您使用的是Java 7,则可以在单个catch中通过管道传递异常:

try {
    // code which can throw many kinds of exceptions
}
catch (FooException | BarException | BazException e) {
    // do the common thing!
}

如果您使用的是Java 7,则可以在单个catch中通过管道传递异常:

try {
    // code which can throw many kinds of exceptions
}
catch (FooException | BarException | BazException e) {
    // do the common thing!
}

如果您使用的是Java 7,您可以将所有这些简化为:

try {
    ...
} catch(RollbackException|HeuristicMixedException|HeuristicRollbackException e) {
    if(log.isErrorEnabled()) {
        log.error(e);
    }

    try {
        trans.rollback()
    } catch(RollbackException|HeuristicMixedException|HeuristicRollbackException|SystemException e1) {
        e1.printStacktrace();
    }
}
如果您使用的是Java6,那么您的选择是有限的。您可以执行
catch(异常e)
,但这将捕获任何类型的异常,这不是您想要的。因此,您必须执行
instanceof
检查,以确保您具有正确的异常类型,然后
抛出不匹配的异常:

try {
    ...
} catch(Exception e) {
    if(e instanceof RollbackException ||
       e instanceof HeuristicMixedException ||
       e instanceof HeuristicRollbackException) {
        if(log.isErrorEnabled()) {
            log.error(e);
        }

        try {
            trans.rollback();
        } catch(Exception e1) {
            if(e1 instanceof RollbackException ||
               e1 instanceof HeuristicMixedException ||
               e1 instanceof HeuristicRollbackException ||
               e1 instanceof SystemException) {
                e1.printStacktrace();
            } else {
                throw e1;
            }
        }

    } else {
        throw e;
    }
}

如果您使用的是Java 7,您可以将所有这些简化为:

try {
    ...
} catch(RollbackException|HeuristicMixedException|HeuristicRollbackException e) {
    if(log.isErrorEnabled()) {
        log.error(e);
    }

    try {
        trans.rollback()
    } catch(RollbackException|HeuristicMixedException|HeuristicRollbackException|SystemException e1) {
        e1.printStacktrace();
    }
}
如果您使用的是Java6,那么您的选择是有限的。您可以执行
catch(异常e)
,但这将捕获任何类型的异常,这不是您想要的。因此,您必须执行
instanceof
检查,以确保您具有正确的异常类型,然后
抛出不匹配的异常:

try {
    ...
} catch(Exception e) {
    if(e instanceof RollbackException ||
       e instanceof HeuristicMixedException ||
       e instanceof HeuristicRollbackException) {
        if(log.isErrorEnabled()) {
            log.error(e);
        }

        try {
            trans.rollback();
        } catch(Exception e1) {
            if(e1 instanceof RollbackException ||
               e1 instanceof HeuristicMixedException ||
               e1 instanceof HeuristicRollbackException ||
               e1 instanceof SystemException) {
                e1.printStacktrace();
            } else {
                throw e1;
            }
        }

    } else {
        throw e;
    }
}

您可以考虑是否所有这些异常都属于同一类型,然后,如果它们属于同一类型,则使它们成为更大超类的子类

编辑:我被告知你没有写这些异常。对于一种迂回的方式,您可以使用以下事实:这些是非运行时异常(因为它们是检查异常)。这可能不是一个好习惯,我建议你重新考虑你的结构

try{
    ...
catch(Exception e){
    if(e instanceof RuntimeException)
        throw e; //rethrow unexpected exception
    //do the right thing
}

关于代码重组,我注意到以下几点:

  • 引发的两个异常是回滚异常,指示事务已回滚
  • 该方法不会抛出除
    SystemException
    之外的任何异常
  • “事务管理器会引发SystemException,以指示它遇到了阻止未来事务服务继续进行的意外错误情况。”()因此,在发生
    SystemException
    后无法回滚
如果捕获到异常,应该是因为您将修复它指示的问题(更一般地说,将程序恢复到有效状态),或者您将执行某些操作(如资源清理或日志记录)并重新播放它,或者您将停止程序

我不知道你在做什么,我建议:

  • 不要在此级别捕获
    SystemException
    。让它更进一步
  • 捕获
    RollbackException
    HeuristicRollbackException
    并记录它们,但不要回滚
  • 捕获
    HeuristicMixedException
    并回滚
我们简化了代码,甚至不知道如何在同一地点捕获多种类型的异常。使用,我们可以通过做您想要的事情来进一步简化:在一个位置捕获多个异常

给你

try{
    try{
        trans.commit();
    } catch (HeuristicMixedException e) {
        if(log.isErrorEnabled()){
            log.error(e);
        }
        trans.rollback(); //catch SystemException at another level
        //Note: We are now leaving this catch block as if
        //  the program state is okay again.
    } catch (RollbackException|HeuristicRollbackException e){
        if(log.isErrorEnabled()){
            log.error(e);
        }
        //don't need to rollback

        //Note: We are now leaving this catch block as if
        //  the program state is okay again.
    }
    //let SystemException propagate
}catch(SystemException e){
    //!do something
    //fix it or rethrow
}

您可以考虑是否所有这些异常都属于同一类型,然后,如果它们属于同一类型,则使它们成为更大超类的子类

编辑:我被告知你没有写这些异常。对于一种迂回的方式,您可以使用以下事实:这些是非运行时异常(因为它们是检查异常)。这可能不是一个好习惯,我建议你重新考虑你的结构

try{
    ...
catch(Exception e){
    if(e instanceof RuntimeException)
        throw e; //rethrow unexpected exception
    //do the right thing
}

关于代码重组,我注意到以下几点:

  • 引发的两个异常是回滚异常,指示事务已回滚
  • 该方法不会抛出除
    SystemException
    之外的任何异常
  • “事务管理器会引发SystemException,以指示它遇到了阻止未来事务服务继续进行的意外错误情况。”()因此,在发生
    SystemException
    后无法回滚
如果捕获到异常,应该是因为您将修复它指示的问题(更一般地说,将程序恢复到有效状态),或者您将执行某些操作(如资源清理或日志记录)并重新播放它,或者您将停止程序

我不知道你在做什么,我建议:

  • 不要在此级别捕获
    SystemException
    。让它更进一步
  • 捕获
    RollbackException
    HeuristicRollbackException
    并记录它们,但不要回滚
  • 捕获
    HeuristicMixedException
    并回滚
我们简化了代码,甚至不知道如何在同一地点捕获多种类型的异常。使用,我们可以通过做您想要的事情来进一步简化:在一个位置捕获多个异常

给你

try{
    try{
        trans.commit();
    } catch (HeuristicMixedException e) {
        if(log.isErrorEnabled()){
            log.error(e);
        }
        trans.rollback(); //catch SystemException at another level
        //Note: We are now leaving this catch block as if
        //  the program state is okay again.
    } catch (RollbackException|HeuristicRollbackException e){
        if(log.isErrorEnabled()){
            log.error(e);
        }
        //don't need to rollback

        //Note: We are now leaving this catch block as if
        //  the program state is okay again.
    }
    //let SystemException propagate
}catch(SystemException e){
    //!do something
    //fix it or rethrow
}

您使用的是Java 6还是Java 7?您可以
捕获(异常e)
然后进行类型检查。如果它不是您期望的类型,请记住抛出
throw第一个捕获没有尝试?你能清理问题中的代码吗?我怀疑您可能会让一些异常逃逸,但我不确定为什么您的catch for rollbacks中有多个回滚。您是否使用Java 6或7?您可能可以
catch(Exception e)
然后执行类型检查。如果它不是您期望的类型,请记住抛出
throw第一个捕获没有尝试?你能清理问题中的代码吗?我怀疑您可能会让一些异常逃逸,但我不确定为什么在回滚捕获中有多个回滚。这些是Java EE异常,因此他无法修改它们。@VivinPalath确认。“这仍然是一个普遍有用的策略。这些是JavaEE例外,所以他无法修改它们。”VivinPalath承认。仍然是一个普遍有用的策略。有趣。那么
e
的类型是什么?最接近的超级类型<代码>例外情况