Learn example of how to write your Custom Exception in Java. Best practice of custom exception Java example with working java program.
1. Step-by-step example of Custom exception in Java
In this blog post, you will learn best practices about writing your custom exception. I have taken example of UserNotFoundException
. By naming convention, this Exception throws when the user not found for provided userId. There are three classes involved in this example as:
UserNotFoundException.java
as custom exception- DAO class
UserDAOImpl.java
and - Java class
UserNotFoundExceptionExample.java
client application which run and gives the output of program.
2. Write Custom Exception
Write your custom exception, e.g. UserNotFoundException.java
which basically used to throw when a user by id not found in DAO (Data Access Object) layer.
There is three constructor methods in this custom exception.
- First constructor, accept Throwable so that you can pass your sub-class of Exception
- Second constructor, accept your custom string message and sub-class of exception to pass super class constructore
- Third constructor, accept only customer string message to pass caller class and call super class constructor
UserNotFoundException.java
package com.mysoftkey.exception; /** * @author ranjeet.jha * */ public class UserNotFoundException extends Exception { /** * @param throwable */ public UserNotFoundException(Throwable throwable) { super(throwable); } public UserNotFoundException( String msg, Throwable throwable) { super(msg, throwable); } public UserNotFoundException( String msg) { super(msg); } }
3. DAO (Data Access Object) class for throwing custom exception
In this section, I will throw custom exception i.e. UserNotFoundException from DAO (Data Access Object ), we should throw exception if user not found by userId. Here, I throws explicitly just to demonstrate how to throws UserNotFoundException
.
UserDAOImpl.java
package com.mysoftkey.exception; import java.util.HashMap; import java.util.Map; /** * @author ranjeet@mysoftkey.com * */ public class UserDAOImpl { /** * @param id * @return * @throws Exception * @throws UserNotFoundException */ public MapgetUserById(int id) throws Exception, UserNotFoundException { Map userMap = new HashMap (); try { // here explicitly throwing exception. throw new UserNotFoundException("User not found for id: " + id); } catch(UserNotFoundException e) { throw e; } catch (Exception e) { } return userMap; } }
4. Custom Exception Client Class
UserManager.java
package com.mysoftkey.exception; import java.util.Map; public class UserManager { public static void main(String[] args) { UserDAOImpl dao = new UserDAOImpl(); try { System.out.println("befor calling to pull user by id."); MapuserMap = dao.getUserById(10); System.out.println(userMap.toString()); System.out.println("After calling to pull user by id."); } catch (Exception e) { // here we get same message which I set while throwing of UserNotFoundException System.err.println("messg: "+ e.getMessage()); e.printStackTrace(); } } }
5. Output of Program
messg: User not found for id: 10 befor calling to pull user by id. com.mysoftkey.exception.UserNotFoundException: User not found for id: 10 at com.mysoftkey.exception.UserDAOImpl.getUserById(UserDAOImpl.java:28) at com.mysoftkey.exception.UserManager.main(UserManager.java:21)
6. Reference
For more details visit Oracle Java Site
I hope, you enjoyed this post about developing custom exception in java. You can visit Core Java tutorial for more example blog post.
Write your comments or suggestion to improve this post. Happy Learning! 🙂