Arsen Alexanyan
Arsen Alexanyan

Reputation: 3141

Assertion VS Runtime exception

I am writing API, so my API will be used from external modules. And here is a one of methods where I can't figure out what to use assertion or java.lang.IllegalArgumentException

/**
 * Adds translation of information to underlying store for particular language
 * @param languageId The identifier of the language 
 * @param translation The translation provided for the specific language
 * @throws AssertionError if the provided language id is {@code null} or empty
 *         or provided translation is {@code null} or empty
 */
public final void addTranslation(String languageId, String translation){
    assert !(Strings.isNullOrEmpty(languageId));
    assert !(Strings.isNullOrEmpty(translation));

    translations.put(languageId, translation);
}

If I use runtime exception, I think that it may harm the execution of the application which is using this API. If I use assertion then it will harm my API, if the assertion flag is disabled.

Also tried to read similar post When to use an assertion and when to use an exception. But it is bit confusing to detect which case is mine.

Is there a strictly defined way, where to use assertions and where to use runtime exceptions?

Upvotes: 5

Views: 4650

Answers (4)

Mikhail
Mikhail

Reputation: 4223

Neither assertions, nor exceptions should be used for implementing business logic. For one simple reason: handling exception is a very slow. Imagine, that your code is invoked very often, then handling bad input, would take too long. Just accepte one contract: check variables before invocation and pass only clean parameter or check variables with if statement in the begining of the method.

UPD:

Assertions should be used to check something that should never happen, while an exception should be used to check something that might happen.

Using Assertions in Java

Assertions throw an error instead of an exception because their purpose is to crash your program.

Class Error

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

AssertionError is subclass of Error

Upvotes: -2

user2102972
user2102972

Reputation: 261

Assertions are only used for testing. For other usage you should use exceptions or if structores (the second solution is preferred as it's much faster than catching exception)

Upvotes: 0

Anton
Anton

Reputation: 6051

Do not use assertion for validation of input data for your API. Simply throw runtime exception, if your API is used incorrectly, for example IllegalArgumentException

Upvotes: 2

duffymo
duffymo

Reputation: 308763

Assertions are usually a development technique that can be switched off in production. That's true in Java, Eiffel, C++, and every language that I know that uses them.

Personally I prefer runtime exceptions to enforce the contract. You can't turn those off.

Upvotes: 4

Related Questions