Java Reference
In-Depth Information
Display 9.10
Use of a throws Clause (part 1 of 2)
1 import java.util.Scanner;
2 public class DivisionDemoSecondVersion
3 {
4 public static void main(String[] args)
5 {
6 Scanner keyboard = new Scanner(System.in);
7 try
8 {
9 System.out.println("Enter numerator:");
10 int numerator = keyboard.nextInt();
11 System.out.println("Enter denominator:");
12 int denominator = keyboard.nextInt();
13 double quotient = safeDivide(numerator, denominator);
14 System.out.println(numerator + "/"
15 + denominator
16 + " = " + quotient);
17 }
18 catch (DivisionByZeroException e)
19 {
20 System.out.println(e.getMessage());
21 secondChance();
22 }
23
24 System.out.println("End of program.");
25 }
26
27
public static double safeDivide( int top, int bottom)
28
throws DivisionByZeroException
29 {
30
if (bottom == 0)
31
throw new DivisionByZeroException();
32
return top/( double )bottom;
33 }
(continued)
 
Search WWH ::




Custom Search