Write a Java program to accept a number from the user and throw an exception if the number is not even number.

import java.io.*;
class NumberException extends Exception
{
 NumberException(String msg)
 {
  super(msg);
 }
}
class Exp8_2
{
 public static void main(String[] args) 
 {
  int n;
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  try
  {
   System.out.println("Enter any number: ");
   n = Integer.parseInt(br.readLine());
   if(n%2!=0)
    throw new NumberException("Number should be even");
   else
    System.out.println("Thanks for entering even number");
  }
  catch(NumberException ne)
  {
   System.out.println(ne);
  }
  catch(IOException ioe)
  {}
 }
}
/*  OUTPUT!!!
Enter any number:
7
NumberException: Number should be even
*/


Copyright © LetML. Blogger Templates Designed by OddThemes