Overview - Checking exception type hierarchy
What is it?
Checking exception type hierarchy means verifying that the code throws the correct kind of error, including its specific type or any of its parent types. In Java, exceptions are organized in a family tree where some exceptions are more general and others more specific. Using JUnit, a popular testing framework, you can write tests that confirm your code throws the expected exception or any exception that inherits from it. This helps ensure your program handles errors properly and predictably.
Why it matters
Without checking the exception type hierarchy, tests might miss subtle bugs where the wrong kind of error is thrown or caught. This can cause programs to behave unpredictably or hide serious problems. By verifying the exact or related exception types, you make your tests stronger and your software safer. It prevents situations where a test passes just because any error was thrown, even if it was the wrong one.
Where it fits
Before this, you should understand basic Java exceptions and how to write simple JUnit tests. After this, you can learn about advanced exception handling, custom exceptions, and how to test asynchronous code that throws exceptions.