【问题标题】:throw checked exception as unchecked in java instead of wrapping在java中将检查的异常抛出为未检查而不是包装
【发布时间】:2021-03-01 05:59:52
【问题描述】:

我有以下代码,我需要扫描抛出的异常。如果它满足特定条件,我将忽略该异常。否则我重新扔。抛出的异常是一个检查异常,这意味着重新抛出是困难的部分。包括我必须抓住它并且操作发生在一个被覆盖的方法中,该方法的基类方法没有使用throws clause。在重新编程整个庞大的库或将异常包装为RuntimeException 之间陷入困境(这不是一个选项,因为我们将包装的检查异常,预计在未来某个地方(派生类)在其传播过程中被一个 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_);
            }
            //
        }
    }

【问题讨论】:

  • 即使您没有捕获并重新抛出异常,这也是一个问题。如果您的方法中的某些内容可以抛出已检查的异常,则您的方法必须要么具有throws 子句,要么捕获异常(而不是重新抛出它)。那么,在您添加所有花哨的异常逻辑之前,这段代码是如何处理已检查异常的呢?
  • @DawoodibnKareem 在我不得不链接一些使用异常作为信号的库以退出并将一些信息传播给“根调用者”之前,它不会引发异常。不幸的是,如果信号数据要到达“根调用者”,我应该采用相同的方法。然后我就卡住了。
  • 好吧,既然您的“根调用者”无法处理这些异常,我建议将它们包装起来,并处理派生类在发生这种情况时必须拦截它们的问题。不过创建自己的 RuntimeException 子类,以便将来开发该拦截类时,它会有一些东西要寻找。

标签: java try-catch throws catch-block rethrow


【解决方案1】:

如果您想抛出未经检查的异常,或者最具体的 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.

【讨论】:

    猜你喜欢
    • 2010-10-03
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多