Chaps, today I was reading about exception handling and although the book explains them in sufficient details there is something that I really struggle to understand. Ok, here's the example they analyze:
// Fig. 11.2: DivideByZeroWithExceptionHandling.java
// Handling ArithmeticExceptions and InputMismatchExceptions.
import java.util.InputMismatchException;
import java.util.Scanner;
public class DivideByZeroWithExceptionHandling
{
// demonstrates throwing an exception when a divide-by-zero occurs
public static int quotient( int numerator, int denominator )
throws ArithmeticException
{
return numerator / denominator; // possible division by zero
} // end method quotient
public static void main( String[] args )
{
Scanner scanner = new Scanner( System.in ); // scanner for input
boolean continueLoop = true; // determines if more input is needed
do
{
try // read two numbers and calculate quotient
{
System.out.print( "Please enter an integer numerator: " );
int numerator = scanner.nextInt();
System.out.print( "Please enter an integer denominator: " );
int denominator = scanner.nextInt();
int result = quotient( numerator, denominator );
System.out.printf( "\nResult: %d / %d = %d\n", numerator,
denominator, result );
continueLoop = false; // input successful; end looping
} // end try
catch ( InputMismatchException inputMismatchException )
{
System.err.printf( "\nException: %s\n",
inputMismatchException );
scanner.nextLine(); // discard input so user can try again
System.out.println(
"You must enter integers. Please try again.\n" );
} // end catch
catch ( ArithmeticException arithmeticException )
{
System.err.printf( "\nException: %s\n", arithmeticException );
System.out.println(
"Zero is an invalid denominator. Please try again.\n" );
} // end catch
} while ( continueLoop ); // end do...while
} // end main
} // end class DivideByZeroWithExceptionHandling
So, my question is with the throw clause: in line 10 the method quotient throws an exception that will be caught by the catch statement.
SO I wonder, why does that method throws an exception and instead none of these nextInt()
System.out.print( "Please enter an integer numerator: " );
int numerator = scanner.nextInt();
System.out.print( "Please enter an integer denominator: " );
int denominator = scanner.nextInt();
do? Shouldn't they throw an inputMismatchException
the same way quotient()
throws its ArithmeticException
? Or, if you prefer, we can turn things around:
can we get rid of the throws ArithmeticException
? So in my head I simply don't understand why one uses a throw clause and the other one doesn't.