Sunday, 12 March 2023

10. Write a program that accepts 5 even numbers from command line , if any of the numbers is odd then throw custom exception OddException and count such invalid numbers.

 

class OddException extends Exception
 {
     OddException(int a)
     {
         System.out.println("number is odd: " + a);
     }
 }
 
 class Program10
 {
     public static void main(String args[])
     {
         int arr[] = new int[5];
         
        try
        {
            for(int i=0; i<5; i++)
            {
                arr[i] = Integer.parseInt(args[i]);
               
                if(arr[i] % 2 != 0)
                    throw new OddException(arr[i]);
            }
        }
        catch(Exception e)
        {
           
        }
     }
 }


No comments:

Post a Comment

python programs

1. sum of two number