User defined Exception subclass in java

User-defined Exception subclass in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.

User-defined Exception subclass

You can also create your own exception subclass simply by extending java Exception class. You can define a constructor for your Exception subclass (not compulsory) and you can override the toString() function to display your customized message on the catch.
User-defined Exception subclass
Add caption

class MyException extends Exception
{
 private int ex;
 MyException(int a)
 {
  ex=a;
 }
 public String toString()
 {
  return "MyException[" + ex +"] is less than zero";
 }
}

class Test
{
 static void sum(int a,int b) throws MyException
 {
  if(a<0)
  {
   throw new MyException(a);
  }
  else { 
   System.out.println(a+b); 
  }
 }

 public static void main(String[] args)
 {
  try
  {
   sum(-10, 10);
  }
  catch(MyException me)
  {
   System.out.println(me);
  }
 }
}

Points to Remember

  1. Extend the Exception class to create your own exception class.
  2. You don't have to implement anything inside it, no methods are required.
  3. You can have a Constructor if you want.
  4. You can override the toString() function, to display a customized message.

Method Overriding with Exception Handling

There are few things to remember when overriding a method with exception handling. If superclass method does not declare any exception, then subclass overridden method cannot declare a checked exception but it can declare unchecked exceptions.
Method Overriding with Exception Handling


Example of Subclass overridden Method declaring Checked Exception

import java.io.*;
class Super
{
 void show() { System.out.println("parent class"); }
}

public class Sub extends Super 
{
 void show() throws IOException  //Compile time error   
  { System.out.println("parent class"); } 

 public static void main( String[] args )
 {
  Super s=new Sub();
  s.show();
 }  
}



As the method show() doesn't throw any exception while in Superclass, hence its overridden version can also not throw any checked exception.

Example of Subclass overridden Method declaring Unchecked Exception

import java.io.*;
class Super
{
 void show(){ System.out.println("parent class"); }
}

public class Sub extends Super 
{
 void show() throws ArrayIndexOutOfBoundsException      //Correct
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  Super s=new Sub();
  s.show();
 }  
}
Output : child class
Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overridden show() method can throw it.

More about Overridden Methods and Exceptions

If a Superclass method throws an exception, then Subclass overridden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by the Super class method.
It means, if a Superclass method throws an object of NullPointerException class, then Subclass method can either throw the same exception or can throw no exception, but it can never throw an object of Exception class (parent of NullPointerException class).

Example of Subclass overridden method with the same Exception

import java.io.*;
class Super
{
 void show() throws Exception 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception  //Correct     
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  
}
Output : child class

Example of Subclass overridden method with no Exception

import java.io.*;
class Super
{
 void show() throws Exception 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show()               //Correct     
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  
}
Output : child class

Example of Subclass overridden method with parent Exception

import java.io.*;
class Super
{
 void show() throws ArithmeticException 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception           //Cmpile time Error     
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  
}
















































































Comments