Search Here

Java program for creating Own Exception


/* Own Exception */

import java.io.*;
class MyException extends Exception
{
 MyException(double val)
 {
  System.out.println("Exception caught: Salary can not be Zero or Negative: "+val);
 }
}

class Main 
{
 static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 public static void main(String args[]) throws MyException 
 {
  int empid;
  String ename;
  double sal;
  
  try
  {
   System.out.println("Enter Employee ID: ");
   empid=Integer.parseInt(br.readLine());
   
   System.out.println("Enter Employee Name: ");
   ename=br.readLine();
   
   System.out.println("Enter Employee Salary: ");
   sal=Double.valueOf(br.readLine());
   if(sal<=0)
    throw new MyException(sal);
  }
  catch(Exception e){}
 }
}

JAVA program for creating Own Exception
Output

No comments:

Post a Comment