Defining Your Own Exception Class in JAVA (Custom Exception)

Defining your own Exception class in JAVA

 
Now you know how to write exception handlers for those exception objects that are thrown by the runtime system, and thrown by a method in the standard class library.
 
It is also possible for you to define your own exception classes and to cause objects of those classes to be thrown whenever an exception occur. In this case, you get to decide just what constitutes an exceptional condition. For example, suppose you could write a data-processing application that processes integer data obtained via a TCP/IP link from another computer. If the specification for the program indicates that the integer value 100 should never be received, then you could use an occurrence of the integer value 100 to cause an exception object of your own design to be thrown.
 
exceptFig1.jpg.gif 
 

Exception Types in Java

 
Mainly Exceptions are of two types; checked and unchecked exceptions:
  • Error and Runtime Exception are unchecked, these exceptions not handled or check by compiler that you handle them explicitly
  • All other Exception are checked that is compiler enforce or check that you can handle them explicitly
An error is an abnormal situation of the JVM (Java Virtual Machine)
  • Running out of memory
  • Infinite recursion
  • Inability to link

Creating Custom Exceptions in Java

  • Use the Exception class in the API.
  • Create a Custom Exception class if the predefined class is not sufficient.
  • Declare a custom exception classes by extending the Exception class or a subclass of Exception.
If you decide to define your own exception class. it must be a subclass of a Throwable class. You must decide which class you will extend.
The two existing subclasses of Throwable are Exception and Error.  
  
Example - Making your own Exception class by using constructor
  1. // this is a user define exception class etend by Exception class
  2. class Myexception extends Exception {  
  3.     public Myexception(int i) {  
  4.         System.out.println("you " + i + " entered It exceeding the limit");  
  5.     }  
  6.   
  7. }  
  8.   
  9. public class ExceptionTest {  
  10.     public void show(int i) throws Myexception {  
  11.         if (i > 100)  
  12.             throw new Myexception(i);  
  13.         else  
  14.             System.out.println(+i + " is less then 100 it is ok");  
  15.     }  
  16.   
  17.     public static void main(String[] args) {  
  18.         int i = Integer.parseInt(args[0]);  
  19.         int j = Integer.parseInt(args[1]);  
  20.         ExceptionTest t = new ExceptionTest();  
  21.         try {  
  22.             t.show(i);  
  23.             t.show(j);  
  24.         } catch (Throwable e) {  
  25.             System.out.println("catched exception is" + e);  
  26.         }  
  27.     }  
  28. }  
OUTPUT
 
customexception cmd.gif
 
Example - Making your own Exception class by using super keyword.
  1. class MyException extends Exception {  
  2.     MyException(String s) {  
  3.         super(s);  
  4.     }  
  5.     public String toString() {  
  6.         return (" " + getMessage());  
  7.     }  
  8. }  
  9. class ThrowClass {  
  10.     int age;  
  11.     ThrowClass(int age) throws MyException {  
  12.         this.age = age;  
  13.     }  
  14.     void getAge(int age) throws MyException {  
  15.         if (age < 18) {  
  16.             throw new MyException("Invalid Age");  
  17.         } else {  
  18.             this.age = age;  
  19.         }  
  20.     }  
  21. }  
  22. class TestException {  
  23.   
  24.     public static void main(String[] args) {  
  25.         int a = Integer.parseInt(args[0]);  
  26.         try {  
  27.             ThrowClass t = new ThrowClass(a);  
  28.             t.getAge(a);  
  29.             System.out.println(t.age);  
  30.         } catch (MyException me) {  
  31.             System.out.println(me);  
  32.         }  
  33.     }  
  34. }  
OUTPUT
 
testException.gif 
 
Resources


Similar Articles