How to create Custom Exception Java Program Example

Custom Exception Java Program Example

In this tutorial, We will show you How to create Custom Exception Java Program Example. This example will make you to understand Custom Exception creation in Java Programming language. The below series of program is tested and its output is posted in the same article.

Employee.java

The Employee Class is simple POJO (Plain Old Java Object) class.

package com.dineshkrish;

public class Employee {

	private int empId;
	private String empName;
	private int empAge;

	public Employee(int empId, String empName, int empAge) {
		this.empId = empId;
		this.empName = empName;
		this.empAge = empAge;
	}

	public int getEmpId() {
		return empId;
	}

	public String getEmpName() {
		return empName;
	}

	public int getEmpAge() {
		return empAge;
	}

}

EmployeeException.java

In order to create Custom Exception in Java. The desired class has to extends java.lang.Exception class. In this example we have EmployeeException which typically extends Exception class. and It has the message attribute to pass appropriate error message time of exception handling.

package com.dineshkrish;

public class EmployeeException extends Exception {

	private String message;
	
	public EmployeeException(String message) {
		
		this.message = message;
	}
	
	public String getMessage() {
		
		return message;
	}
}

EmployeeService.java

The EmployeeService is actual business logic. we are assuming its going to perform database insertion, But before that Its doing typical validation based on the business requirements.

package com.dineshkrish;

public class EmployeeService {

	public void addCustomer(Employee employee) throws EmployeeException {
		
		// Performing the validation before Inserting to DB
		
		if(employee == null) { // Validation 1
			
			throw new NullPointerException(); // Predefined Exception 
		}
		
		if(employee.getEmpId() <= 0) { // Validation 2
			
			throw new EmployeeException("Invalid Employee ID"); // Custom Exception
		}
		
		if(employee.getEmpAge() <= 0) { // Validation 3
			
			throw new EmployeeException("Invalid Employee Age"); // Custom Exception
		}
		
		// All other exception you can include based on the business requirement.
		
		// Database Operation goes here...
		
		// Printing Success message if no exception
		System.out.println("Hi "+employee.getEmpName()+" your information added successfully !!!");
		
	}
}

EmployeeApplication.java

This class in the Entry point of the application, Since it has the main() method. The output of the EmployeeApplication is attached in the same post.

package com.dineshkrish;

public class EmployeeApplication {
	
	public static void main(String[] args) {

		EmployeeService service = new EmployeeService();
		
		Employee employee1 = new Employee(1001, "John", 30);
		Employee employee2 = new Employee(1002, "Smith", -35);
		
		try {
			
			System.out.println("Processing... employee1 object");
			System.out.println("-------------------------------");
			// Processing the employee1 object
			service.addCustomer(employee1);
			
			System.out.println();
			
			System.out.println("Processing... employee2 object");
			System.out.println("-------------------------------");
			
			// Processing the employee2 object
			service.addCustomer(employee2);
			
			
		} catch (EmployeeException e) {
			
			System.out.println(e.getMessage());
		}
			
	}
}

Output

Processing… employee1 object
——————————-
Hi John your information added successfully !!!

Processing… employee2 object
——————————-
Invalid Employee Age