write a program to show user defined Exception in the following condition: you have to enter the salary of a person at runtime and the salary must be a non-negative number & should be in between 6,000 & 20,000. and if it is not it will throw a user defined exception named.

class MyException extends Exception
{
 private int salary;

 MyException(int s)
  {
  salary=s;

  }
 public String toString()
  {
    return "MyException[" + salary +"]";
  }
 }
class ExceptionDemo
 {
  static void compute(int s) throws MyException
  {
   System.out.println("main exception(" + s + ")");
   if((s>60000)&&(s<20000))
   throw new MyException(s);
   System.out.println("no exception");
  }
 public static void main(String args[])
  {
  try
   {
   int s=Integer.parseInt(System.console().readLine("Enter the salary :"));
   compute(s);
   }
   catch(MyException e)
   {
      System.out.println("caugth;"+e);
   }
  }
 }

No comments:

Post a Comment