Best practices for Exception handling

Friends, we have been using exception handling in our code for almost every industry standard application. Usual approach is to create some custom Exception classes extending the base Exception class. In this post, I will suggest another approach which might make you more sense.

This post is part of series related to best practices and mini guides.

Sections in this post:

  • Our new approach
  • Advantages

Our new approach

Probably some of us are already using this mechanism. If you are one of them, then please review this post and suggest the improvements. Otherwise, learn about this mechanism and give your feedback.

Our new approach uses static inner classes for every new exceptional scenario. Traditionally, we create a DBException class by extending Exception class. Now, every time we are caught in a situation where there needs to thrown exception, we usually create an instance of this class, put some information in form of message and throw it.

Now, lets consider there are following situations we have identified in which we need to throw DBException:

  1. SQL execution error
  2. No data exist where we expect at least one row
  3. Multiple rows exist where we expect only single row
  4. Invalid parameters error

and many more such cases can exist.

Different with our traditional approach, let try our new approach i.e. create static inner classes.

Lets start with BaseException class which is created as abstract and will be super class of all our exception classes.

// Make this class abstract so that developers are forced to create
// suitable exception types only
public abstract class BaseException extends Exception{
	//Each exception message will be hold here
	private String message;

	public BaseException(String msg)
		this.message = msg;
	//Message can be retrieved using this accessor method
	public String getMessage() {
		return message;

Not its time to create out new Exception inner classes. Let hit the keyboard.

public class DBExeption
	//SQL execution error
	public static class BadExecution extends BaseException
		private static final long serialVersionUID = 3555714415375055302L;
		public BadExecution(String msg) {
	//No data exist where we expect at least one row
	public static class NoData extends BaseException
		private static final long serialVersionUID = 8777415230393628334L;
		public NoData(String msg) {
	//Multiple rows exist where we expect only single row
	public static class MoreData extends BaseException
		private static final long serialVersionUID = -3987707665150073980L;
		public MoreData(String msg) {
	//Invalid parameters error
	public static class InvalidParam extends BaseException
		private static final long serialVersionUID = 4235225697094262603L;
		public InvalidParam(String msg) {

Here, we created an inner class for each possible error scenario identified in starting. There can be many more extra. It depends on you only to identify and add more classes.

Let see how these classes look like in log files.

public class TestExceptions {
	public static void main(String[] args)
			throw new DBExeption.NoData("No row found for id : x");
		catch(Exception e)

com.exception.DBExeption$NoData: No row found for id : x
at com.test.TestExceptions.main(

As you can see the log message in exception stack trace, if has become more informative.

Advantages of using inner classes

  1. Foremost advantage is that if you developer has written some doubtful message text, then also you can clearly observe that what was actually wrong.
  2. You can use instance-of comparison in different situation where you handle different exceptional scenarios.
  3. You don’t need to send single exception for a large set of exceptional conditions.
  4. Its easy to write unit test cases for negative cases where you know the exact exception class, you should expect.
  5. Logging is more meaningful and informative.

I hope this post has been some informative for you. If you have some suggestions, please write to me.

Happy Learning !!

15 thoughts on “Best practices for Exception handling”
  1. Creating specific exception types is good idea but grouping them in a Class is not better (at least for me). This is against design principle “Strong Cohession”. Are classes really intended for grouping types or it can be done better with packages ?

    Moreover, using inner classes leads to memory leaks !

    1. Packaging them in groups is also a good idea if developers are intelligent enough for using correct class in case of any exception.
      Memory leak? I don’t think so. Can you please elaborate more on memory leak issue?

  2. Hey Lokesh,

    thanks for writing this article. Reminding developers of being mindful with exception handling is a good idea, as it’s too often done in a sloppy way.

    I have a few remarks though:

    * BaseException reinvents the wheel: the class “Exception” already has a way to store a message; no need to duplicate that in BaseException. Just pass the message parameter on to the superclass constructor.

    * BaseException has no constructor that takes a cause-exception (BaseException(String message, Throwable cause)). This is important in case you’re throwing a BaseException (or derived class) as a result of catching another exception. By specifying the original exception as cause, it’s much easier to debug what really happened as if you’re logging the stacktrace, you’ll get information about the inner exception as well.

    A good point you’re making is that having detailed exceptions is often more helpful than using one generic exception for everything. But there’s one thing I don’t get: why do you put them into DBException, as static inner classes? Why not just putting them top-level, without a wrapping class. You get exactly the same advantages of having detailed exceptions, without the weirdness of having so many inner classes.

    1. My intention is to group related exception classes in one class, just like we use packages. We can create some classes denoting broad exception categories and then use inner classes to create specific failure cases.

      Apart from above, code example is more to represent idea. Anyone can refine the code as per his/her need.

      1. correct me if I’m wrong, but aren’t you using the outer classes just to create a new namespace?

        why do you group your exceptions in one class and not a package? Is it to circumvent the Java restriction to have multiple classes in the same file?

        1. Yes, I am trying to create a logical namespace.

          You want to group them in package.. no problem..It will still be beneficial.. at least more than having a generic exception for a group of failures.

  3. Don’t you think this will create classes for each and every static inner class, which is a huge, if we keep on adding for every error one static class. Pls let me know if am wrong and suggest me..

    1. Yes, effort is more than usual. But rewards are far better. Believe me, I have used this technique in one project and it works like a charm. Also, you do not need to teach every tiny detail of exception handling logic to team members, most of the things are self-explanatory. This is highly recommended if you are building a product which you will be managing for 5-10 years.

      1. although there are many classes created, but still I believe these classes are accessible just like a method on an object with out a need to create an object for it, other than the parent class like as you shown “new DBExeption.NoData(“No row found for id : x”);”, which will not load JVM with more objects.

        But, I have a doubt, if the growing no of errors increases to support “Logging is more meaningful and informative.”, does that not make the object huge for jvm.

        what happens if there are many scenarios and you created/covered all those in your product/application, where u create an object every time, for exception handling, making JVM fully loaded with many huge DBExeption objects.

        So, do we have to make this “DBExeption” class a singleton?

        Pls let me know if am wrong with my assumption.

        1. If such thing happen when there are plenty of exceptions all the time, then application will crash anyway. No fault of object creation logic or JVM load.
          Regarding singleton. I believe we should treat exception objects as first class citizen in our application. If there is an exception occurred, let’s have a fresh new exception object and populate it with complete context information. They have same importance in your application as much DTO objects.
          “DBExeption class as a singleton?”. Need thorough review but its possible.

        2. Please never, ever try to use singletons as exceptions! Here’s why:
          * When an exception instance is created, its constructor (the one in class Throwable) automatically captures the stack trace, so you can later (e.g. in a log) find out where excactly the exception was thrown.
          If you’re reusing exception instances, you lose this information. You will always get the stacktrace showing where the instance was originally created, giving you now way to find out where it was actually thrown.

          Regarding your worries about memory:
          Exception instances are not huge. Maybe you are worried about the size of the class DBException in this example. But there’s no need to be. First of all, you’ll not throw instances of this class, but only of its inner classes (which again are rather small). Second, With static inner classes, there’s no real connection between the outer and the inner class. In fact, on the level of the classloader, there’s nearly no difference between a static inner class and a “normal” class.
          And third: exception instances are usually short-lived, so the garbage collector will remove them pretty soon.

          In the end, I think you’ll never see a case where an application runs into out-of-memory problems because of exceptions.

          1. Thanks(Rolf Schäuble) for a detailed Information.

            Also, I would like to know in a real time scenario, how and when actually the JVM triggers the grabage collector. is there any specific config changes that we have to do on JVM for this.


Leave a Reply
