-1

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

D. Sikilai
  • 467
  • 1
  • 3
  • 17
  • 1
    Even if you weren't catching and rethrowing the exception, this would be a problem. If something in your method can throw a checked exception, your method must EITHER have a `throws` clause, OR catch the exception (and not rethrow it). So how did this code deal with checked exceptions before you added all your fancy exception logic? – Dawood ibn Kareem Nov 18 '20 at 00:03
  • @DawoodibnKareem It was not throwing exceptions until i had to link some library that used exceptions as signals to exit and propagate some info to the 'root caller'. Which unfortunately indicated that i should adopt the same method if the signal data were to reach the 'root caller'. Then i got stuck. – D. Sikilai Nov 18 '20 at 00:13
  • Well since your "root caller" is not able to deal with these exceptions, I'd recommend wrapping them, and dealing with the issue of the derived class having to intercept them when that happens. Make your own RuntimeException subclass though, so that in the future when that intercepting class is developed, it will have something to look for. – Dawood ibn Kareem Nov 18 '20 at 00:30

2 Answers2

2

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.

0

I needed something similar, but my method was expecting a return value, which caused me to return null or something similar.

Thus, I made a small adaption:

private static <T extends Throwable> RuntimeException unchecked(Throwable t) throws T {
    throw (T) t;
}

The method is now returning a RuntimeException, which allows us to "throw" the unchecked exception, which bypasses the compiler, and does not longer expect to return any value.

private static String yourIOOperationThatReturnsSomething() {
    throw unchecked(new IOException("test"));
}

public static void main(String []args) {
    System.out.println(yourIOOperationThatReturnsSomething());
}

A running example can be found here: http://tpcg.io/VIC7A6

Andi
  • 21
  • 1