I got this method:
public void foo(){
int test = 5/0;
}
And I call it like this:
try {
foo();
} catch (Exception e){
e.printStackTrace();
}
This catches the ArithmeticException
, but I have seen cases where I call a method inside try-catch
, but it doesn't catch the exception. Is there a specific situation in which the exception
may not be caught?
Here is an example to clarify the question:
Does the try-catch block in ActivityA
catches the exception in ActivityB
? I know the answer is negative. But I want to know the logic behind this.
public class ActivityA extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
startActivity(new Intent(this, ActivityB.class));
} catch (Exception e){
e.printStackTrace();
}
}
}
And ActivityB
:
public class ActivityB extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
int x = 5/0
}
}
To all cool guys who think this is a duplicate question, please give me a link to a question on StackOverFlow that answers to this very clear question: Does a try-catch catches exceptions that happens in a method which is called inside it?