A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Custom Exception

Custom Exception

What is a custom exception in java ?

In java, we have a default package named as java.lang package, which contains a throwable class. The Throwable class is the superclass of all errors and exceptions in the Java language.

What is a custom exception class?

  • Custom exceptions give us the flexibility to add methods that are not part of a predefined exceptions.

  • It is used to store the additional information it is also called user defined Exception.

  • When you couldn’t find any relevant exceptions in the JDK then we use custom exceptions

  • To create a custom exception, we have to extend the java.lang.Exception class.

  • We want more readable error messages or string messages, so that developers can understand the code issue while coding.

  • We can throw the custom exception by using throw keyword.

  • InvalidAgeException, LowScoreException, InvalidUserStateException, TooManyStudentsException this type of exceptions are custom exceptions.

  • We want more readable error messages or string messages, so that developers can understand the code issue while coding.

  • All predefined child exceptions class extends exception public.

  • Custom unchecked exception means you will get the exception at runtime not at compile time.

  • Sometime, we see the main method throws the exception like public static void main (string args) extends exception class.

The thing that we use to use a custom exception class i.e. We have to implement a checked and unchecked exception and need to extend the class Exception.

How to use Custom Exceptions

  • Create a class the name of the class like TestExcepton.

  • Now extends the class from one of the exceptions which are subtypes of the java.lang.Exception class.

  • A custom exception class always extends directly from the Exception i.e. predefined class.

  • Create a constructor with a String parameter which is the detail message of the exception. In this constructor, simply call the super constructor and pass the message.

Program for custom Exception:-





import java.util.Scanner;
class TestCustomException extends Exception 
{

   public TestCustomException(String data) 
   {
     super(data);
   }

}
class Test2 
{

   public void checkAge()throws TestCustomException
   {  
     Scanner src=new Scanner(System.in);
   
     System.out.println("Enter your age");
     int age=src.nextInt();
  
     if(age < 18)
     {    
        throw new TestCustomException("U are not eligible for vote");  
     }
     else  
     {
        System.out.println("U are eligible for vote"); 
     }    
   }  
     
   public static void main(String args[])
   {  
     Test2 obj=new Test2();
     try
     {  
        obj.checkAge();  
     }
     catch(Exception ex)
     {
        System.out.println(ex);
     }   
  
     System.out.println("Rest of the normal code free of exceptions...");  
   }  
}


OutPut 1:-
Enter your age
32
U are eligible for vote
Rest of the normal code free of exceptions...

OutPut 2:-
Enter your age
12
TestCustomException: U are not eligible for vote
Rest of the normal code free of exceptions...


 











LEARN TUTORIALS

.

.