Write a java program to create three userdefined exceptions and throw the exceptions using throw and write appropriate catch and finally blocks to handle.

 /* Userdefine Exceptions 28-12-2021 */

import java.util.*;
class WrongBranchCodeExcetion extends Exception
{
    WrongBranchCodeExcetion(String s1)
    {
    super(s1);
    }
}

class WrongBranchNameExcetion extends Exception
{
    WrongBranchNameExcetion(String s2)
    {
    super(s2);
    }
}

class WrongRollNumberExcetion extends Exception
{
    WrongRollNumberExcetion(String s3)
    {
    super(s3);
    }
}

class UserDefinedException_5B
{
    public static void main(String args[])  
    {  
    try
        {
         int b;
         Scanner s = new Scanner(System.in);
         System.out.print("Enter Branch Code : ");
         b = s.nextInt();
         if(b != 5)
         {
            throw new WrongBranchCodeExcetion("You are not belong to CSE");
         }
       
         String branch;
         System.out.print("Enter Branch Name : ");
         branch = s.next();
         if(branch.compareTo("CSE") != 0)
         {
            throw new WrongBranchNameExcetion("You are entered wrong branch name");
         }

         String rno;
         System.out.print("Enter Roll Number : ");
         rno = s.next();
         if(rno.length() != 10)
         {
            throw new WrongRollNumberExcetion("You are entered wrong roll number");
         }
        }
        catch(WrongBranchCodeExcetion e)
        {
         System.out.println(e);
        }
    catch(WrongBranchNameExcetion e)
        {
         System.out.println(e);
        }
    catch(WrongRollNumberExcetion e)
        {
         System.out.println(e);
        }
finally
{
   System.out.println("finally block is executed");
    }
}
}

Comments

Popular posts from this blog

Design GUI to handle Choice Control event.

Write a Java program that reads an integer number (between 1 and 255) from the user and prints the binary representation of the number. The answer should be printed as a String. Note: The output displayed should contain 8 digits and should be padded with leading 0s(zeros), in case the returned String contains less than 8 characters. For example, if the user enters the value 16, then the output should be 00010000 and if the user enters the value 100, the output should be 01100100 (Hint : You may use String.format() method for the expected output)