Skip to content

Java Quickie: Exceptions In Unfinished Code

Delivering a library that isn’t completed (for alpha testing)? Learn how to handle exceptions in unfinished code with this Java Quickie.

Why should you throw exceptions in unfinished code, and why? Let me illustrate this by an example:

You are creating a calculator (how creative) and have the skeleton of the class already set up. The add, subtract and divide methods are fully implemented with the according logic. multiply isn’t. If a Tester tries to multiply two numbers he doesn’t get an error because there is no fault in the logic. But he doesn’t get a result either. He has no idea what’s going on.

Wouldn’t it be more useful for testers to know what’s going on there?

Exceptions In Unfinished Code UnsuportedOperationException

If you implement the UnsupportedOperationException in unfinished methods:

  • the user will get a message
  • you have a reminder of pending work, too
  • it could even give an explanation for testers

Example:

public int multiply(int n1, int n2) {

    throw new UnsupportedOperationException("Multiplication of two numbers isn't implemented yet, because I was distracted watching Mr. Robot");

}

Now a tester or user of your calculator will know what should have happened. He also knows why it didn’t. Kinda like a real exception. This will be especially relevant if you release an unfinished class or product for testing.

Another option would be the NotImplementedException. But you’ll need the Lava/ Sharkysoft packages for that one.

Published inQuickiesSoftware Dev

Be First to Comment

Leave a Reply

%d bloggers like this: