Try the program below to see the error when N is 0.
avgFirstN()method expects that N will be greater than 0. If N happens to be 0, an error will occur in the expression
sum/N, because you cannot divide an integer by 0. Try it to see this error.
avgFirstN()method takes the traditional approach to error handling: Error-handling code is built right into the algorithm. If N happens to be 0 when
avgFirstN()is called, the following output will be generated:
ERROR avgFirstN: N <= 0. Program terminating.
avgFirstN()method is passed an argument of 0 in the
CalcAvgTest.main(). When the JVM detects the error, it will abort the program and print the following message:
Exception in thread "main" java.lang.ArithmeticException: / by zero at CalcAverage.avgFirstN(Compiled Code) at CalcAvgTest.main(CalcAvgTest.java:5)
CalcAverage.avgFirstN()method, which was called by the