How to handle multiple exceptions in Java

1 Answer

0 votes
import java.util.InputMismatchException;
import java.util.Scanner;

class Program {
    public static void main(String[] args) {
        try	{
			Scanner sc = new Scanner(System.in);
			System.out.print("Enter A Number : ");
			int x = sc.nextInt();
			
			int y = 0;
			int z = x / y;
			
			System.out.println("Result  = " + z);
		}
		catch(InputMismatchException e) {
			System.out.println("Invalid Input...");
		}
		catch(ArithmeticException e) {
			 System.out.println("Error:Divide By ZERO");
        }
    }
}




/*
run 1:

Enter A Number : asdf
Invalid Input...

*/


/*
run 2: 

Enter A Number : 26
ERROR!
Error:Divide By ZERO

*/

 



answered Jan 24, 2024 by avibootz

Related questions

...