❮ Previous Next ❯

Customized Exception

When the exception is defined by the user based on the user’s requirement is known as User defined Exception or Customized Exception. Let’s see the following program for better clarity:

CustomizedDemo.java
package java8s;

import java.util.*;

class TooOldException extends RuntimeException
{
      TooOldException(String s)
      {
            super(s);
      }
}
class TooYoungException extends RuntimeException
{
      TooYoungException(String s)
      {
            super(s);
      }
}
public class CustomizedDemo {

      public static void main(String[] args) {
            int age;
            Scanner s=new Scanner(System.in);
            System.out.println("enter the value of age");
            age=s.nextInt();
            if(age>60)
            {
                  throw new TooOldException("sry!!!we can't help u");
            }
            else
                  if(age<18)
                  {
                        throw new TooYoungException("sry!!u have 2 wait some yrs");
                  }
                  else
                  {
                        System.out.println("Thanks a ton...");
                  }

      }

}

 

1st Run Output

customize-handling1

2nd Run Output

customize-exception2

3rd Run Output

customize-exception3.PNG

❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved