TheAlgorithms/Java · error · IllegalArgumentException
Denominator cannot be 0
Error message
Denominator cannot be 0
What it means
ADTFraction is a Java record representing a fraction; its compact constructor rejects denominator == 0 because division by zero is undefined. Since records validate in the constructor, any creation of an ADTFraction with denominator 0 throws — including internal calls from plus(), times(), and reciprocal() if those produce a zero denominator.
Source
Thrown at src/main/java/com/thealgorithms/maths/ADTFraction.java:13
package com.thealgorithms.maths;
public record ADTFraction(int numerator, int denominator) {
/**
* Initializes a newly created {@code ADTFraction} object so that it represents
* a fraction with the {@code numerator} and {@code denominator} provided as arguments.
*
* @param numerator The fraction numerator
* @param denominator The fraction denominator
*/
public ADTFraction {
if (denominator == 0) {
throw new IllegalArgumentException("Denominator cannot be 0");
}
}
/**
* Add two fractions.
*
* @param fraction the {@code ADTFraction} to add
* @return A new {@code ADTFraction} containing the result of the operation
*/
public ADTFraction plus(ADTFraction fraction) {
var numerator = this.denominator * fraction.numerator + this.numerator * fraction.denominator;
var denominator = this.denominator * fraction.denominator;
return new ADTFraction(numerator, denominator);
}
/**
* Multiply fraction by a number.
*View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate denominator != 0 before constructing; for reciprocal, check numerator != 0 first.
- When taking input, reject or prompt again if the denominator is 0.
- In arithmetic chains, guard reciprocal() callers against zero numerators.
Example fix
// before
ADTFraction f = new ADTFraction(3, 0);
ADTFraction r = someFraction.reciprocal(); // fails if someFraction.numerator()==0
// after
if (denominator == 0) throw new IllegalArgumentException("denominator must be non-zero");
ADTFraction f = new ADTFraction(3, denominator);
if (someFraction.numerator() == 0) throw new ArithmeticException("cannot invert zero fraction");
ADTFraction r = someFraction.reciprocal(); Defensive patterns
Strategy: validation
Validate before calling
if (denominator == 0) {
throw new IllegalArgumentException("denominator cannot be 0");
}
new ADTFraction(numerator, denominator);
// for reciprocal:
if (f.numerator() == 0) {
throw new ArithmeticException("cannot take reciprocal of a zero fraction");
}
f.reciprocal(); Prevention
- Validate denominator != 0 at the input boundary before constructing fractions.
- Guard reciprocal() callers: a zero numerator becomes a zero denominator after the swap.
- When doing chained arithmetic, be aware that overflow could yield a zero denominator in plus/times.
When it happens
Trigger: Constructing new ADTFraction(n, 0), or calling reciprocal() on a fraction whose numerator is 0 (reciprocal swaps num/den, yielding denominator=0). Also times(0) internally constructs ADTFraction(0,1) then multiplies — safe — but a plus/times producing a zero denominator via arithmetic is theoretically possible with overflow.
Common situations: Calling reciprocal() on ADTFraction(0, 5) (zero numerator), user input with denominator 0, or a computation that yields a zero denominator through cancellation/overflow.
Related errors
- capacity must greater than 0!
- Capacity must be greater than 0!
- Capacity cannot be negative.
- initialCapacity < 1
- Numbers array cannot be empty or null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/af021be24b47adf4.
Report an issue: GitHub.