Urim Ibrahimi Urim Ibrahimi - 9 months ago 49
Java Question

try catch exception while loop

I would like the program to re-do the while loop when it catches the exception - the exception being receiving a number zero. Instead it continues a while loop with the code below, I would like it to ask for the user input again until the user inputs a number that is different by zero.

import java.util.InputMismatchException;
import java.util.Scanner;

public class whilePerjashtim {

public static int division(int a, int b){

return a/b;

public static void main(String[] args) {

Scanner s = new Scanner(System.in);
int a, b;

System.out.println("Enter a value: ");
a = s.nextInt();



System.out.println("Enter b value");
b = s.nextInt();

System.out.println("Sum of division is: " + division(a,b));


catch(ArithmeticException e)
System.err.println("Don't divide by zero!!!");

catch (java.util.InputMismatchException e)
System.err.println("Enter just a Number!!!");





Use something of the following form (not exact Java for your homework problem)

 boolean validInput = false;
 while (!validInput) {
    .. get input
       .. set validInput = true if no error
    .. catch error 
       .. print try again message