Package pro.teamlead.demo.sonatype
Class IntAdder
java.lang.Object
pro.teamlead.demo.sonatype.IntAdder
The
Component class provides utility methods for arithmetic operations.
This class demonstrates how to handle arithmetic overflow by throwing an exception.-
Method Summary
Modifier and TypeMethodDescriptionstatic intsum(int a, int b) Sums two integers and checks for overflow.
-
Method Details
-
sum
public static int sum(int a, int b) Sums two integers and checks for overflow.If adding the two integers results in an overflow, this method throws an
ArithmeticException. This method demonstrates how to manually handle overflow conditions in integer arithmetic, which is not automatically checked by Java's standard arithmetic operations.- Parameters:
a- the first operandb- the second operand- Returns:
- the sum of
aandb - Throws:
ArithmeticException- if the result overflows the range of integers
-