简体   繁体   English

未捕获由lambda抛出的Java未经检查的异常

[英]Java unchecked exception thrown by lambda is not caught

I have a function that takes two lambdas as a parameters. 我有一个函数,它将两个lambda作为参数。 These functions throw a specific unchecked exception that I want the function to catch: 这些函数抛出一个我想要函数捕获的特定未经检查的异常:

    /**
     *
     * @param insert function to insert the entity
     * @param fetch function to fetch the entity
     * @param <T> type of entity being inserted
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
    public <T> T getOrInsertWithUniqueConstraints(Supplier<Optional<T>> fetch, Supplier<T> insert) {
        try {
            Optional<T> entity = fetch.get();
            T insertedEntity = entity.orElseGet(insert);
            return insertedEntity;
        }
        catch (Exception e){
            //I expect/want the exception to be caught here, 
            //but this code is never called when debugging
            Optional<T> entityAlreadyInserted = fetch.get();
            return entityAlreadyInserted.get();
        }
    }

Which is called in a function belonging to another transaction: 在属于另一个事务的函数中调用它:

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
...
try {
    Player persistedPlayer = insertOrGetUtil.getOrInsertWithUniqueConstraints(
        () -> playerRepository.findOne(newPlayer.getUsername()),
        //this lambda throws the unchecked DataIntegrityViolationException
        () -> playerRepository.save(newPlayer)
    );
}
catch (Exception e){
    //the exception is caught here for some reason...
}

Am I misunderstanding how Java lambdas work? 我误解了Java lambdas是如何工作的吗? Also worth noting is the code is using Spring's @Transactional and CrudRepository 另外值得注意的是代码使用的是Spring的@TransactionalCrudRepository

The exception is actually happening while the transaction is being committed, which occurs after the method returns. 事务正在提交时实际发生异常,该异常在方法返回后发生。 To get around this, I used EntityManager#flush() to trigger any exceptions that would happen on commit before the method returns: 为了解决这个问题,我使用了EntityManager#flush()来触发在方法返回之前在提交时发生的任何异常:

    @Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
    public <T> T getOrInsertWithUniqueConstraints(Supplier<Optional<T>> fetch, Supplier<T> insert) {
        try {
            Optional<T> entity = fetch.get();
            T insertedEntity = entity.orElseGet(insert);
            entityManager.flush();
            return insertedEntity;
        }
        catch (PersistenceException e){
            DataAccessException dae = persistenceExceptionTranslator.translateExceptionIfPossible(e);
            if (dae instanceof DataIntegrityViolationException){
                Optional<T> entityAlreadyInserted = fetch.get();
                return entityAlreadyInserted.get();
            }
            else {
                throw e;
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果未选中 Java 则停止 bash 脚本 抛出异常 - Stop bash script if unchecked Java Exception is thrown 是不是在try块中捕获的未经检查的异常在Java中检查了异常? - Isn't an unchecked exception that is caught in a try block a checked exception in Java? 预期的异常未捕获或未抛出? - Expected exception not caught or not thrown? Java:未报告的异常Exception; 必须被抓住或宣布被扔掉 - Java: Unreported exception Exception; must be caught or declared to be thrown 在Spring的@Transactional注释的方法中引发并捕获的未经检查的异常是否还会导致事务回滚? - Will an unchecked exception thrown and caught inside a method annotated with spring's @Transactional still cause the transaction to rollback? 未抛出异常时捕获异常 - Exception is caught when Exception is not thrown 未报告的异常java.sql.SQLException;必须被抓或宣布被扔? - Unreported exception java.sql.SQLException; must be caught or declared to be thrown? Java错误:必须捕获未声明的异常ioexception或将其声明为引发 - Java error: unreported exception ioexception must be caught or declared to be thrown 未报告的异常java.lang.ClassNotFoundException; 必须被抓住或宣布被抛出 - unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown 未报告的异常 ParseException; 必须被捕获或声明被抛出 -- JAVA 错误 - unreported exception ParseException; must be caught or declared to be thrown -- JAVA Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM