Compiler error: Unreachable catch block
When doing test-driven development and writing your expectations before the actual production code, you may get the error Compiler error: Unreachable catch block. In this case, one has used a try-catch block with a checked exception, but the try-block does not contain a method that throws that exception. Thus the compiler can conclude, that the try-catch block is unessary. If you get this error, you simply add the missing throws clause. For example, when you have the following test:
try {
libApp.addBook(book1);
fail();
} catch (OperationNotAllowedException e) { .. }
and you get the error message Compiler error: Unreachable catch block, you just add the throws OperationNotAllowedException to addBook(Book b).
Part of User-defined Exceptions
Hubert Baumeister
April 23, 2018