VOOZH about

URL: https://www.javacodegeeks.com/2020/03/call-that-an-exception.html

⇱ Call That An Exception? - Java Code Geeks


While this is a Java example to do with testing and wiremock, it relates to a more universal problem.

We were trying to retry Wiremock’s verify method, which may be called by our test before the endpoint we’re checking is hit. In that situation, we’d want to try again a few seconds later in a loop until timing out. Interesting, the Wiremock client doesn’t provide a method like this, but meh, they’re easily created.

👁 Image

The type of object thrown was called VerificationException so we wrote something like this:

01
02
03
04
05
06
07
08
09
10
for (int i=0; i<maxRetries; i++) {
    try {
        verify... // try to verify
        return;   // verify ok
    } catch (Exception e) {
        LOG.info("Oooh, it went wrong on try " + i);
        // let the loop run it again after a sleep
        sleep(1000);
    }
}

It didn’t work. Our catch block wasn’t hit.

Digging deeper, and always read the source code of the open source libraries you use, it seems like the VerificationException is derived from AssertionError.

An Error is not an Exception. So why is the VerificationException not called the VerificationError? Our catch block needed to catch Error or Throwable to work. Which it now does and it does work.

What Went Wong?

This is a case of violating the principle of least surprise. Because the thrown object was called exception, nobody would imagine that it was anything else. We needed to write a failing exception catcher, debug it, and read a couple of classes deep in the source code to find this mistake. Was it our mistake for expecting an exception to be an exception?

You can easily explain why they chose the misleading name, but if you have to explain something that violates the norm, then you would be better putting the effort in to make it not require an explanation.

Published on Java Code Geeks with permission by Ashley Frieze, partner at our JCG program. See the original article here: Call That An Exception?

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Ashley Frieze
Ashley Frieze
March 6th, 2020Last Updated: March 4th, 2020
3 572 1 minute read

Ashley Frieze

Software developer, stand-up comedian, musician, writer, jolly big cheer-monkey, skeptical thinker, Doctor Who fan, lover of fine sounds
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
anonymout
6 years ago

It was your mistake to expect VerificationException to be Exception. Misleading or not, catch the expected exception instead of using Exception.

0
Reply
6 years ago
Reply to  anonymout

In some situations that may be true. In this situation, we were writing a general purpose retry strategy, which needed to retry all exceptions. The fact that one of the advertised exceptions was an Error was an unexpected move on the part of the author of that class.

0
Reply
6 years ago

Hi…
I’m Elena gillbert.An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system. … This block of code is called an exception handler.

-2
Reply
Back to top button
Close
wpDiscuz