Darshan K
Darshan K

Reputation: 11

Runtime Error handling

class A {
    B b = new B();
    b.myMethod();
}

class B {
    public void myMethod() {
        C c = new C();
        c.errMethod();
    }
}

class C {
    public int errMethod() {
        // Some runtime Error
    }
}

Some runtime error in errMethod(). My application should not shutdown. What should I do here?

This is one of the Java interview question, we should not be using any exception handling technique. Is there any other way to handle this?

Upvotes: 0

Views: 161

Answers (1)

Narendra Rajput
Narendra Rajput

Reputation: 711

Write the code in errMethod in a try/catch block.

public int errMethod(){

   try{
    //code goes here
   }
   catch(Exception e){
    // handle exception
   }
}

Upvotes: 2

Related Questions