Saturday 31 December 2011

User defined Exception in java

Create the below given java classes.
Class Name :exceptionTest.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class exceptionTest{
                        public static void main(String arg[]) throws MainException, NumberFormatException, IOException{
                                                //This is a example for user defined exception
                                                System.out.print("Please enter Employee Number form 1 to 10 : ");
                                                InputStreamReader istream = new InputStreamReader(System.in) ;         
                                                BufferedReader bufRead = new BufferedReader(istream) ;
                                                int userEntry = Integer.parseInt(bufRead.readLine());
                                                if(userEntry > 10){
                                                   throw new EmpNotFoundException("Please Enter a value from 1 to 10");
                                                }else{
                                                                        System.out.println("You have entered the value: "+ userEntry);
                                                }
                        }
}

Class Name : MainException.java

 public class MainException extends Exception {

                        private static final long serialVersionUID = -6452702088031438499L;
                        private String errorKey;
                         
                        public void setErrorKey(String errorKey) {
                                                this.errorKey = errorKey;
                        }
                       
                        public String getErrorKey() {
                                                return errorKey;
                        }

                        public MainException() {
                                                super();
                        }

                        public MainException(String errorKey, Throwable cause) {
                                                super(errorKey,cause);
                                                this.errorKey = errorKey;
                        }

                        public MainException(String errorKey) {
                                                super(errorKey);
                                                this.errorKey = errorKey;
                        }

}


Class Name : EmpNotFoundException.java
  
public class EmpNotFoundException extends MainException {

                        private static final long serialVersionUID = 1L;
                        private String errorKey;
                       
                        public void setErrorKey(String errorKey) {
                                                this.errorKey = errorKey;
                        }
                       
                        public String getErrorKey() {
                                                return errorKey;
                        }
                       
                        public EmpNotFoundException(String errorKey, Throwable cause) {
                                                super(errorKey, cause);
                        }

                        public EmpNotFoundException(String errorKey) {
                                                super(errorKey);
                        }

}

Now run the exceptionTest.java. If you enter a value greater than 10, then user defined exception will be triggered and the message appears in the console.
Try it !!!

No comments:

Post a Comment