2023-09-29 10:11:43 +02:00
|
|
|
This issue indicates that an exception will be thrown because a method is used incorrectly.
|
|
|
|
To fix it, check the requirements of the method and fulfill them.
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-08-23 10:10:34 +02:00
|
|
|
It is common for methods to check the value of their parameters or the state of
|
|
|
|
their associated object and throw an exception when one of them does not match a
|
|
|
|
given condition.
|
2022-03-29 16:09:37 +02:00
|
|
|
Those conditions are usually mentioned in the javadoc of the method.
|
|
|
|
|
2023-08-23 10:10:34 +02:00
|
|
|
This rule raises an issue when it detects that a method call will trigger one of
|
|
|
|
the following exceptions:
|
|
|
|
|
2023-09-29 10:11:43 +02:00
|
|
|
* `java.lang.IllegalArgumentException`
|
|
|
|
* `java.lang.IllegalStateException`
|
|
|
|
|
|
|
|
=== What is the potential impact?
|
|
|
|
|
|
|
|
include::../../../shared_content/layc/exception-impact.adoc[]
|
2022-03-29 16:09:37 +02:00
|
|
|
|
2023-09-29 10:11:43 +02:00
|
|
|
== How to fix it
|
2022-03-29 16:09:37 +02:00
|
|
|
|
2023-09-29 10:11:43 +02:00
|
|
|
=== Code examples
|
2022-03-29 16:09:37 +02:00
|
|
|
|
2023-09-29 10:11:43 +02:00
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
2022-03-29 16:09:37 +02:00
|
|
|
----
|
2023-09-29 10:11:43 +02:00
|
|
|
/**
|
|
|
|
* Set the oven temperature
|
|
|
|
* @param temp the temperature in Celsius, between 0 and 250 (inclusive)
|
|
|
|
* @throws IllegalArgumentException if the temperature is outside of the supported range
|
|
|
|
*/
|
2024-08-26 12:54:45 +02:00
|
|
|
private void setOvenTemperature(int temp) {
|
2023-09-29 10:11:43 +02:00
|
|
|
if (temp < 0 || temp > 250) {
|
|
|
|
throw new IllegalArgumentException();
|
2022-03-29 16:09:37 +02:00
|
|
|
}
|
2023-09-29 10:11:43 +02:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2024-08-26 12:54:45 +02:00
|
|
|
void finishCooking() {
|
|
|
|
setOvenTemperature(-3); // Noncompliant
|
2023-09-29 10:11:43 +02:00
|
|
|
}
|
|
|
|
----
|
2022-03-29 16:09:37 +02:00
|
|
|
|
2023-09-29 10:11:43 +02:00
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
|
|
----
|
|
|
|
/**
|
|
|
|
* Set the oven temperature
|
|
|
|
* @param temp the temperature in Celsius, between 0 and 250 (inclusive)
|
|
|
|
* @throws IllegalArgumentException if the temperature is outside of the supported range
|
|
|
|
*/
|
2024-08-26 12:54:45 +02:00
|
|
|
private void setOvenTemperature(int temp) {
|
2023-09-29 10:11:43 +02:00
|
|
|
if (temp < 0 || temp > 250) {
|
|
|
|
throw new IllegalArgumentException();
|
2022-03-29 16:09:37 +02:00
|
|
|
}
|
2023-09-29 10:11:43 +02:00
|
|
|
// ...
|
|
|
|
}
|
|
|
|
|
2024-08-26 12:54:45 +02:00
|
|
|
void finishCooking() {
|
|
|
|
setOvenTemperature(0);
|
2023-09-29 10:11:43 +02:00
|
|
|
}
|
2022-03-29 16:09:37 +02:00
|
|
|
----
|
|
|
|
|
2023-09-29 10:11:43 +02:00
|
|
|
== Resources
|
|
|
|
|
|
|
|
=== Documentation
|
|
|
|
|
|
|
|
* https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalArgumentException.html[IllegalArgumentException]
|
|
|
|
* https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalStateException.html[IllegalStateException]
|