首页 > 代码库 > Java 异常之 try catch finally--除数不为0

Java 异常之 try catch finally--除数不为0

 1 package com.intel.aug6; 2  3 import java.util.InputMismatchException; 4 import java.util.Scanner; 5  6 public class TestException2 { 7  8     public int divsion(int f1, int f2) { 9         return f1 / f2;10     }11 12     /*13      * 在java除法中,允许浮点数运算时除 数为零,所得结果是Infinity14      */15     public void inputdata() {16         Scanner reader = new Scanner(System.in);17         try {18             System.out.println("Enter value1");19             int f1 = reader.nextInt();20             System.out.println("Enter value2");21             int f2 = reader.nextInt();22 23             int div = divsion(f1, f2);24             // System.out.println(Float.POSITIVE_INFINITY);25             System.out.println("div=" + div);26         } catch (ArithmeticException e) {27 28             System.out.println("0 can‘t be a divisor");29             inputdata();30         } catch (InputMismatchException e) {31 32             System.out.println("you enter the wrong value");33             inputdata();34         } finally {35             System.out.println("Thank you!");36         }37     }38 39     public static void main(String[] args) {40         TestException2 ex = new TestException2();41         ex.inputdata();42     }43 44 }