Checked exceptions (ie, a class which extends Exception
but not RuntimeException
or Error
) thrown by a method should be handled by this method's caller, recursively so.
Here you have .peek()
which throws, say, exception class E
(bad name, but this is for illustration). You must do one of the following in a foo()
method which calls .peek()
:
catch
it, or- throw it again.
That is, either:
// catch
void foo()
{
try {
peek();
} catch (E e) {
// something
}
}
or:
// throw
void foo() throws E
{
peek();
}
You could even rethrow it:
// catch then rethrow
void foo() throws E
{
try {
peek();
} catch (E e) {
// something, then
throw e;
}
}