InputMismatchException

In this blog we work on InputMismatchException with try and catch handler.

This is another program on try catch exception handler with InputMismatchException.

When user put some text in integer value that time this run time error will generate.

Example : We set contact number field as number, but user put some text on field that time this error will generate.

 

import java.util.*;      

class Exc3

{

            public static void main(String args[])

            {

                        Scanner in1 = new Scanner(System.in);

 

                        int x=0,y=0;

                        try

                        {

                                    System.out.print(“Enter X : “);

                                    x = in1.nextInt();

                                    System.out.print(“Enter Y : “);

                                    y = in1.nextInt();

                        }          

                        catch(InputMismatchException ex)

                        {

                                    System.out.println(ex);

                        }

                       

                        try // we already handle ArithmeticException Here

                        {

                                    int div = x/y; // divided by zero :-> infinity

                                    System.out.println(“Div = ” + div);

                        }

                        catch(Exception ex)

                        {

                                    //System.out.println(ex); // Run time simple message

                                    // Change in Simple User friendly Message

                                    System.out.println(“Please do not insert 0 in Y”);

                        }          

 

                        // Simple display 5 name in o/p screen

                        String name[] = {“Rakesh”,”Rohan”,”Binny”,”Sonal”,”Nilay”};

                        for(String str : name)

                        {

                                    System.out.println(str);

                        }

            }

}

Output:

Output -1

Enter X: 23

Enter Y: 12

Div = 1

Rakesh

Rohan

Binny

Sonal

Nilay

 

Output -2

Enter X: 45

Enter Y: sometext

java.util.InputMismatchException

Please do not insert 0 in Y

Rakesh

Rohan

Binny

Sonal

Nilay

 

In this another try catch block program, we find run time error when user put wrong value in Y variable.

 

Video available on YouTube now.


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


Earn money online

175total visits,2visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image