Java - exception handling - sum of numbers - input mismatch exception


/*
Write a java program for exception handling:
To add two integers and raise exception when any other character except number (0 – 9) is given as input.
*/

// java Exception Handling
// input mismatch exception

import java.util.*;

class inputmismatchexceptions
{
     public static void main(String args[])
     {
          int x, y;
          Scanner s = new Scanner(System.in);
         
          try
          {
              System.out.print("Enter first number : ");
              x = s.nextInt();
              System.out.print("Enter second number : ");
              y = s.nextInt();
             
              System.out.print("Sum = "+(x+y));
             
          }
          catch(Exception e)
          {
              System.out.println(e) ;
          }
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu