1 //a beautiful example2 ImportJava.util.Scanner;3 Importjava.util.InputMismatchException;4 5 Public classdevidebyzerowithoutnoexception1{6 Public Static intQuotient (intNumerator,intdenominator)7 throwsArithmeticException8 {9 returnnumerator/denominator;Ten } One A Public Static voidMain (string[] args) { -Scanner scanner=NewScanner (system.in); - the Booleancontinueloop=true; - - Do { - Try + { -System.out.print ("Please enter an int numerator:"); + intNumerator=scanner.nextint (); A atSystem.out.print ("Please enter a in denominator:"); - intDenominator=scanner.nextint (); - - intresult=quotient (numerator,denominator); - -System.out.printf ("%nresult:%d/%d=%d%n", Numerator,denominator,result); in -continueloop=false; to + } - the Catch(inputmismatchexception inputmismatchexception) * { $System.err.printf ("%nexception:%s%n", inputmismatchexception);Panax Notoginseng scanner.nextline (); -System.out.printf ("%nyou must enter an integer number. Please try again.%n%n "); the } + A Catch(arithmeticexception arithmeticexception) the { +System.err.printf ("%nexception:%n%s", arithmeticexception); -System.out.printf ("%nzero is an invalid denom inator! Please try again.%n%n "); $ } $ - -} while(continueloop); the - Wuyi } the}
Devidebyzerowithoutnoexception--from "Java How to Program" (Early Objects), 10th