Q:

Java program to handle multiple exceptions

0

Given two integer numbers and we have to divide them. If is there any exception we are handling with exception handling processes.

Example:

    Input:
    Enter First Number : 10
    Enter Second Number : 20
    Output:
    Result:0
    Explanation: There is no error 10 is divisible by 20

    Input:
    Enter First Number : 100
    Enter Second Number : 0

    Output:
    Error: Divide By ZERO
    Expiation: A number is not divisible by 0, so error is occurred

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Java program:

package ExceptionHandling;

import java.util.Scanner;
import java.util.InputMismatchException;
class ExMultipleCatchBlock
{    
	public static void main(String arg[])
    {  
		try
		{
			// create object of scanner class.
			Scanner KB=new Scanner(System.in);
			
			// enter both the numbers for operation.
			System.out.print("Enter First Number : ");
			int x=KB.nextInt();

			System.out.print("Enter Second Number : ");
			int y=KB.nextInt();
			int z=x/y;
			
			// show the result.
			System.out.println("Result:"+z);
		}
		catch(InputMismatchException e)
		{
			// show if value is invalid.
			System.out.println("Invalid Input...");}
			catch(ArithmeticException e)
		{
			// show when number is divided by 0.
			System.out.println("Error:Divide By ZERO");
		}
    }
}

Output

First run:
Enter First Number : 10
Enter Second Number : 20
Result:0

Second run:
Enter First Number : 50
Enter Second Number : 12
Result:4

Third run:
Enter First Number : 100
Enter Second Number : 0
Error:Divide By ZERO

Fourth run:
Enter First Number : 10.52
Invalid Input...

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now