Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to resume Java execution after exception?

Tags:

java

exception

I have a ParentClass in a JAR, but not source code. I am implementing a SubClass, but i need to handle some corner cases.

class ParentClass {
    void foo() {
       … // lots of code 1
       ; // can possibly throw NullPointerException 
       … // lots of code 2
    }
}
class SubClass extends ParentClass {
    @Override
    void foo() {
       try {super.foo();}
       catch(NullPointerException npe) {… /*handle exception*/}
       finally {… /* resume lots of code 2 ? */}
    }
}

Is there a way to run the //lots of code 2 part after handling the exception in the overriding method? I don't want to duplicate code, and cannot modify the ParentClass.

P.S: The NullPointerException problem wasn't there in the ParentClass. But due to a different instantiation in SubClass, this problem may arise.

like image 742
garyF Avatar asked Sep 07 '17 08:09

garyF


People also ask

How do you continue after catching an exception?

By putting a BEGIN-END block with an exception handler inside of a loop, you can continue executing the loop if some loop iterations raise exceptions. You can still handle an exception for a statement, then continue with the next statement. Place the statement in its own subblock with its own exception handlers.

Will the program continue after catching the exception?

If an exception is thrown inside the catch-block and that exception is not caught, the catch-block is interrupted just like the try-block would have been. When the catch block is finished the program continues with any statements following the catch block.

What happens after exception in Java?

After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred.

How do you continue a loop after catching exception in try catch?

How do you continue a loop after catching exception in try catch Python? Use a for-loop to continue catching exceptions with a try-except block. Place a try-except block inside of a for-loop to continue catching exceptions with the try-except block.


2 Answers

No.

You cannot just jump back into the middle of a method.

If you don't want to copy-paste the code in there (good call!) you have to put the shared code into a separate method that your subclass can call.

Or you could put the part that might throw the NullPointerException into a separate method and override it in the subclass (so that it no longer throws).

But due to a different instantiation in SubClass, this problem may arise.

Maybe you can sidestep the exception altogether by changing the way you do this instantiation? Maybe provide a "dummy object" for the thing that is currently null? Something that doesn't do anything harmful, but prevents the exception?

like image 93
Thilo Avatar answered Oct 21 '22 07:10

Thilo


As others pointed out, you cannot move back there. However, you might be able to refactor Parent.foo() to something like this:

class ParentClass {
    protected void foo() { //  made it protected so it's overridable
       stuffBeforeNPE(); // Extract Method Refactoring
       codeWithPossiblyNPE(); // Extract Method Refactoring
       stuffAfterNPE(); // Extract Method Refactoring
    }

    protected void stuffBeforeNPE() { ... } // you might want to add params and return values
    protected void codeWithPossiblyNPE() { ... }
    stuffAfterNPE() { ... }
}

Now, your child class might look like this:

class SubClass extends ParentClass {
    @Override
    protected void foo() {
      stuffBeforeNPE();
       try {
          codeWithPossiblyNPE();
       } catch(NullPointerException npe) {
          … /*handle exception*/
       }
       stuffAfterNPE();
    }
}
like image 39
Tamas Rev Avatar answered Oct 21 '22 07:10

Tamas Rev