Java Custom Exception

If you are creating your own Exception that is known as custom exception or user-defined exception. Java custom exceptions are used to customize the exception according to user need.

By the help of custom exception, you can have your own exception and message.

class InvalidAgeException extends Exception{  
 InvalidAgeException(String s){  
  super(s);  
 }  
}  

Example

 class TestCustomException{  
  
   static void validate(int age)throws InvalidAgeException{  
     if(age<20)  
      throw new InvalidAgeException("not valid");  
     else  
      System.out.println("welcome to vote");  
   }  
     
   public static void main(String args[]){  
      try{  
      validate(13);  
      }catch(Exception m){System.out.println("Exception occured: "+m);}  
  
      System.out.println("rest of the code...");  
  }  
}  
 

Output: Exception occurred: InvalidAgeException:not valid

       rest of the code...

ExceptionHandling with MethodOverriding in Java

The Rules are as follows:

•   If the superclass method does not declare an exception
Ο   If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.
•  If the superclass method declares an exception
Ο   If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

1) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.

2) Rule: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception

If the superclass method declares an exception

If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent exception.

Example in case subclass overridden method declares parent exception

 import java.io.*;

class Parent {
	void msg() throws ArithmeticException {
		System.out.println("parent");
	}
}

class TestExceptionChild2 extends Parent {
	void msg() throws Exception {
		System.out.println("child");
	}

	public static void main(String args[]) {
		Parent p = new TestExceptionChild2();
		try {
			p.msg();
		} catch (Exception e) {
		}
	}
}
	

	 Output: Compile Time Error
					

Example in case subclass overridden method declares same exception

 import java.io.*;

class Parent {

	void msg() throws Exception {
		System.out.println("parent");
	}
}

class TestExceptionChild3 extends Parent {
	void msg() throws Exception {
		System.out.println("child");
	}

	public static void main(String args[]) {
		Parent p = new TestExceptionChild3();
		try {
			p.msg();
		} catch (Exception e) {
		}

	}
}
 Output: child
 

Example in case subclass overridden method declares no exception

 import java.io.*;

class Parent {

	void msg() throws Exception {
		System.out.println("parent");
	}

}

class TestExceptionChild5 extends Parent {
	void msg() {
		System.out.println("child");
	}

	public static void main(String args[]) {
		Parent p = new TestExceptionChild5();
		try {
			p.msg();
		} catch (Exception e) {
		}
	}

}
 

 Output: child