throw checked exception as unchecked而不是wrapping

y4ekin9u  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(426)

我有以下代码,我需要扫描抛出的异常。如果它满足一个特定的条件,我就忽略这个例外。否则我再扔。被抛出的异常是一个已检查的异常,这意味着重新抛出是困难的部分。包括这样一个事实:我必须捕获它,并且操作发生在一个被重写的方法中,该方法的基类'方法没有使用 throws clause . 在重新编程整个庞大的库或将异常 Package 为 RuntimeException (这不是一个选项,因为我们将 Package 的检查异常,在其传播过程中,catch子句在将来的某个地方(派生类)是预期的-它是某种信号),我希望得到关于这种实现的一些建议。或者只是执行。

/*
     * 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_);
            }
            //
        }
    }
mv1qrgav

mv1qrgav1#

如果要抛出未检查的异常 Throwable 你可以用下面的方法。

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

调用该方法不会导致编译检测到任何错误 Unchecked Exception 错误。

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

输出:It works.

相关问题