Employee Management System Handling Exception
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Overview
The Employee Management System that we developed, works functionally; however, it needs to handle the exceptions properly.
Prerequisite
- User should refer to this code that we’ll work upon.
Handle Exception
The current test report for the employee management system has some failures.

- test_delete_employee is failing because the expected status code is 204, but our API returned 200.
- test_create_employee is failing because it expects status code 201, but our API returned 200.
- test_GetEmployee_employeeNotFound is failing because our service throws the exception EmployeeNotFoundException, which is not handled in the web layer.
Let's go ahead and fix each of them.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Fix Delete Employee API
Spring boot always defaults to the status code 200 OK if API does not return the status code explicitly. To return specific HTTP code, spring boot provides annotation @ResponseStatus, which should be applied to the handler method. Our code change with the fix will be.
Fix Create New Employee
The same change applies to creating an employee method.
Applying the above changes leaves us with only one failure.

Turn Learning into Career Growth
Fix Employee Not Found Exception
Here, we need to use controller advice to handle exceptions and return meaningful content to the user. We need to return a custom response to the user. Let's do that.
1. Create a custom response POJO.
2. Write controller advice
The above code intercepts the exception and returns the response to the user with the status code 404, which goes well with REST.
Our latest test report.

The actual response to the API caller

Complete source code is available at GitHub location under tag version_7.0.0.
Conclusion
In this article, we have
- Improved employee management system to be more RESTful.
- Handled exceptions and returned a meaningful response to the user.




