Okay, here is what I am asked to do by my teacher. Write a basic program that uses one of the arithmetic exceptions (other than dividing a number by zero) modeling the programs you’ve seen in this lesson. Then, in a second program, write one that would not produce an error.
The one’s in the lesson were DivideByZeroException, NotFiniteNumberException, and OverflowException.
EDIT: I cannot use DivideByZeroException.
I am a beginner so I don’t know much about programming. I’m taking a high school class, so please be patient.
Bala’s answer comes close:
This would indeed overflow, but it doesn’t compile because the compiler detects that we’re trying to assign a value that doesn’t fit inside an integer.
If we pass a variable to it, then the compiler doesn’t make any assumption about the value of that variable, so the code compiles. It will fail with
OverflowExceptionat runtime.