Was hoping for an explanation as to what it means to pass an exception up the calling chain by declaring the exception in my methods throws clause and why I would want to do tha
You can do it for example by not catching it in main()
, but passing it to the piece of logic that called main()
. In this case, it's trivial, as main()
is your program's entry point...
You could also rewrite your method
void checkArray() throws ABException {
for (int i = 0; i < charArray.length; i++) {
check(charArray[i]);
}
}
void check(char c) throws ABException {
switch (c) {
case 'a':
throw new ABException();
case 'b':
throw new ABException();// creating the instance of the
// exception anticipated
default:
System.out.println(c + " is not A or a B");
}
}
Now it becomes more clear how checkArray()
"passes the exception from check()
up the calling chain"