简体   繁体   English

在java中将已检查的异常抛出为未检查而不是包装

[英]throw checked exception as unchecked in java instead of wrapping

I have the following code and i need to scan the Exception thrown.我有以下代码,我需要扫描抛出的异常。 If it satisfies a particular condition i ignore the exception.如果它满足特定条件,我将忽略异常。 Else i re-throw.否则我重新扔。 The exception being thrown is a checked exception that means re-throwing is the hard part.抛出的异常是经过检查的异常,这意味着重新抛出是困难的部分。 Including the fact that i had to catch it and the operation is taking place inside an overridden method whose base class' method did not use the throws clause .包括我必须捕获它并且操作发生在基类方法未使用throws clause的重写方法中的事实。 Stuck between reprogramming the whole enormous library or wrapping the exception as RuntimeException (Which is not an option, as the checked exception we will have wrapped, is expected somewhere in future (derived class) by a catch clause during its propagation - its some kind of signal), i hope to get some advice on such an implementation.在重新编程整个庞大的库或将异常包装为RuntimeException之间陷入RuntimeException (这不是一个选项,因为我们将包装的检查异常,预计在未来某个地方(派生类)在传播过程中通过 catch 子句 - 它是某种信号),我希望得到一些关于这种实现的建议。 Or maybe just the implementation.或者也许只是实现。

    /*
     * Translated from c++11_7.3.19 AIEDs schemes >${ref[213]:`through}.
     * guarantees learning, re-learning and 'un-learning'. programmed intelligence is not
     * altered whatsover and is made as root of bias [x:~y]. fetch cycle is omitted.
     */
    @Override
    public void intelligenceSync()// No throws clause here
    {
        try {
            super.intelligenceSync();
            // the throwing culprit.
            global_contribution.acceptOrReformState(this);
            generateMetaLogicReforms(this);
        } catch (Throwable t_) {
            if (t_ instanceof Signalx) {
                Signalx sx = (Signalx) t_;
                // Note how bias inreases propagation speed by ~12.21 >${ref[371]:exp2}.
                applyBias(sx);
                stopInvalidation(sx);
                // check if x neuron is almost proved.
                if (sx.neuronSP() > sx.threshold()) {
                    // We'll find other ways of completing our proofs.
                    // Note the algorithm is not so complete.
                    netsync(sx);
                    while (sx.pushDeeper()) {
                        sx.enhance(Metrics.COMPLETION.esteem());
                        sx.suspendSubPathTraversal(Suspender.IN_DREAM_RESOLVE, Handler.NULL_LOGIC);
                    }
                    generateSubLogicReforms(sx);
                } else {
                    restore(sx);
                    continueInvalidation(sx);
                    // We rethrow.
                    uncheckedThrow(sx);
                    // exception thrown
                }
            } else if (t_ instanceof Signaly) {
                // Reforms must be handle explicitly.otherwise RelationalAttender will complain
                // .
                // ... blah blah blah.
            } else {
                // We rethrow
                uncheckedThrow(t_);
            }
            //
        }
    }

If you want to throw unchecked exceptions or most specifically Throwable you can do that with the following method.如果您想抛出未经检查的异常或最具体的Throwable您可以使用以下方法来做到这一点。

private static <T extends Throwable> void uncheckedThrow_helper(Throwable e) throws T{
   throw (T)e;
}
static void uncheckedThrow(Throwable e){
    uncheckedThrow_helper(e);
}

Calling the method does not cause the compile to detect any Unchecked Exception error.调用该方法不会导致编译检测到任何Unchecked Exception错误。

try{
  uncheckedThrow(new Exception());
  System.out.println("It doesn't work");
}catch(Exception e){
  System.out.println("It works.");
}

Output:输出:

It works.

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM