I’m new to exceptions handling in JAVA. I have some basic question about it:
Inside First.java:
public void foo ()
{
Second obj = new Second();
obj.boo();
}
Inside Second.java
public void boo()
{
try { /* may throw some I/O Exception here */ }
catch (IOException e) { System.err.println(e.message()); }
catch (Exception e) { System.err.println(e.message()); }
}
My question is, do we need to(Should we) add try/catch block inside the foo() method of First.java as well?
Since you are catching the exception inside the boo() method you don’t have too.
But instead if you would have throwed the exception for the calling method to handle it you would have to handle the exception there..
Still if you don’t want to handle the exception inside the foo() method you could choose to throw it again:
Again the method calling the foo() would have to handle the exception otherwise, it would result in an exception