Search code examples
javatry-catchthrowscatch-blockrethrow

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


Solution

  • If you want to throw unchecked exceptions or most specifically Throwable you can do that with the following method.

    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.

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

    Output:

    It works.