I need to know how to handle the exceptions in a situation like below. Please assist me,
public interface DCommand {
public Object execute(Class_A car);
}
public class Class_B {
public void getMessage() throws Exception {
throw new Exception("Test error");
}
}
public class Class_A {
Class_B cb = null;
public Class_B getClass_b() {
cb = new Class_B();
return cb;
}
public Object testAction(DCommand command) {
Object returnObject = null;
try {
return (Boolean) command.execute(this);
} catch (Exception e) {
System.out.println("ERROR IN CLASS B" + e.getLocalizedMessage());
}
return returnObject;
}
}
====================== simiulating ============================
public class Test {
public static void main(String[] args) {
Class_A c = new Class_A();
boolean a = (Boolean) c.testAction(new DCommand() {
@Override
public Object execute(Class_A car) {
try {
car.getClass_b().getMessage();
return true;
} catch (Exception ex) {
System.out.println("Error in the simulator.");
}
return false;
}
});
}
}
When I run the above code I need to catch the exception thrown by the Class_B in the Class_A where prints the "ERROR IN CLASS A".