Updates
  • Starting New Weekday Batch for Full Stack Java Development on 15 September 2025 @ 02:00 PM to 04:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM
Join Course

Java Full Stack Developer Training Center in Noida

User-Define Exception

In the prior tutorials we have seen different built-in exception such as java.lang.NullPointerException, java.lang.ArithmeticException, java.io.FileNotFoundException and etc.

But you know we can create our own exception as per our business logic is called User-Define exception.

Types of User-Define Exception:

a. User-Define Checked Exception
b. User-Define Unchecked Exception

So, in this tutorial first we will discuss how we will create User-Define checked exception in Java.

Ques:- How we can create a User-Define Checked Exception?

• As we discussed earlier In Java every exception is a class and all those exception which are not the sub class of java.lang.RuntiemException are CheckedException or Compile-Time Exception.
So, in order to create User-Define Checked Exception we must have to create a class which must not be the sub class of java.lang.RuntimeException, that class should be sub class of java.lang.Exception directly or indirectly.

            
class StudentNotFoundException extends Exception{

	public String toString(){
		return "Student Not Found";
	}
}

In this example as we can see there is one simple class StudentNotFoundException which is extending java.lang.Exception it means it is a Checked Exception, it means when that exception occurs then we must have to handle it explicitly using try-catch block or we need to use throws keyword.

            
class JTC{
	public static void main(String arg[]){
		Utility.createStudent(101,"Vivek","Noida");
		Utility.createStudent(102,"Rahul","Delhi");
		Utility.createStudent(103,"Amit","Kanpur");
		try{
			Student s1 = Utility.searchStudent(Integer.parseInt(arg[0]));
			System.out.println(s1);
		}catch(StudentNotFoundException e){
			System.out.println(e);
		}
	
	}
}
class Student{
	int sid;
	String sname;
	String scity;

	public Student(int sid, String sname, String scity){
		this.sid = sid;
		this.sname = sname;
		this.scity = scity;
	}

	public String toString(){
		return "[ sid : "+sid+" | sname : "+sname+" | scity : "+scity+" ]";
	}
}
class Utility{
	static int count = 0;

	static Student database[] = new Student[5];
	
	public static void createStudent(int sid, String sname, String scity){
		if(count < database.length){
			database[count] = new Student(sid,sname,scity);
			count++;
			System.out.println("Student Created");
		}else{
			System.out.println("Database is full");
		}
	}
	
	public static Student searchStudent(int sid) throws StudentNotFoundException{
		Student s1 = null;
		for(int i = 0; i <= database.length-1; i++){
			if(database[i] != null){
				if(database[i].sid == sid){
					s1 = database[i];
				}
			}
		}
		if(s1 == null){
			throw new StudentNotFoundException();
		}else{
			return s1;
		}	
	}
}
class StudentNotFoundException extends Exception{

	public String toString(){
		return "Student Not Found";
	}
}
    C:\Users\Rahul\Desktop>javac test.java

    C:\Users\Rahul\Desktop>java JTC 101
    Student Created
    Student Created
    Student Created
    [ sid : 101 | sname : Vivek | scity : Noida ]

    C:\Users\Rahul\Desktop>java JTC 102
    Student Created
    Student Created
    Student Created
    [ sid : 102 | sname : Rahul | scity : Delhi ]

    C:\Users\Rahul\Desktop>java JTC 107
    Student Created
    Student Created
    Student Created
    Student Not Found
            

The above example designed on the basis of a use-case so before goes to the example explanation we must have to understand the given use-case first.

Use-Case:
Write a program to create the student and save into the database (an Array) then search Student by Student Id (sid), if Student found then print Student details on console otherwise throw StudentNotFoundException(a user-define checked exception).

So as per the use-case first we have created a method createStudent() in Utility class:

public static void createStudent(int sid, String sname, String scity)
Which is dedicated to create the Student class object along with data which are given as argument and store into Student type array database [].
Then we have one more method in same class searchStudent().
public static Student searchStudent(int sid) throws StudentNotFoundException
which is dedicated to search the Student in database array if Student found then return Student class object to caller otherwise it throw StudentNotFoundException as we can see the method implementation.
Because StudentNotFoundException is a checked exception so we have thrown form the searchStudent() method header and we are handling the exception in main method of JTC class using try-catch block otherwise we will get UnreportedException at the time of compilation.

Ques:- How to create User-Define Unchecked Exception or Runtime Exception?

As we know all the Unchecked Exception or Runtime Exception are the sub classes of java.lang.RuntimeException, so in order to create User-Define Unchecked Exception we will create one simple class and inherits java.lang.Runtime class directly or indirectly.

            
class StudentDatabaseFull extends RuntimeException{
	
	public String toString(){
		return "Student Database Full.";
	}
}

In the above example as we can see we have a class StudentDatabaseFull which is extending java.lang.RuntimeException, so as we discussed it is an UncheckedException so there is no mandatory rules to handle that exception explicitly.

            
class JTC{
	public static void main(String arg[]){
		Utility.createStudent(101,"Vivek","Noida");
		Utility.createStudent(102,"Rahul","Delhi");
		Utility.createStudent(103,"Amit","Kanpur");
		Utility.createStudent(104,"Neha","Pushkar");
		Utility.createStudent(103,"Arun","Patna");
		Utility.createStudent(103,"Arun","Patna");
		try{
			Student s1 = Utility.searchStudent(Integer.parseInt(arg[0]));
			System.out.println(s1);
		}catch(StudentNotFoundException e){
			System.out.println(e);
		}
	
	}
}
class Student{
	int sid;
	String sname;
	String scity;

	public Student(int sid, String sname, String scity){
		this.sid = sid;
		this.sname = sname;
		this.scity = scity;
	}

	public String toString(){
		return "[ sid : "+sid+" | sname : "+sname+" | scity : "+scity+" ]";
	}
}
class Utility{
	static int count = 0;

	static Student database[] = new Student[5];
	
	public static void createStudent(int sid, String sname, String scity){
		if(count < database.length){
			database[count] = new Student(sid,sname,scity);
			count++;
			System.out.println("Student Created");
		}else{
			throw new StudentDatabaseFull();
		}
	}
	
	public static Student searchStudent(int sid) throws StudentNotFoundException{
		Student s1 = null;
		for(int i = 0; i <= database.length-1; i++){
			if(database[i] != null){
				if(database[i].sid == sid){
					s1 = database[i];
				}
			}
		}
		if(s1 == null){
			throw new StudentNotFoundException();
		}else{
			return s1;
		}	
	}
}
class StudentNotFoundException extends Exception{

	public String toString(){
		return "Student Not Found";
	}
}

class StudentDatabaseFull extends RuntimeException{
	
	public String toString(){
		return "Student Database Full.";
	}
}
    C:\Users\Rahul\Desktop>javac test.java

    C:\Users\Rahul\Desktop>java JTC 101
    Student Created
    Student Created
    Student Created
    Student Created
    Student Created
    Exception in thread "main" Student Database Full.
            at Utility.createStudent(test.java:44)
            at JTC.main(test.java:8)
            

This example written on the same use-case which we have followed in the last example, but there is only one difference is that we have a Unchecked Exception which is StudentDatabaseFull which is thrown by the createStudent() method when we try to store more than 5 [database.length] Student into database Array and because StudentDatabaseFull exception is a Unchecked Exception so no need to handle it explicitly or thrown from the createStudent() method header.