Either you are writing a program in Java or .NET, the questions remains that why to create custom exceptions in the first place? I, in my initial years as IT professional, also kept wondering on why, at all, there is a need for custom exceptions whenever I use to come across one of these types. At the same time, I also wondered on how to come up with right names for custom exceptions if at all I want to create one. Please feel free to add to the below reasons if you believe that there are other points that should also be covered as part of benefits for creating custom exceptions.
Following are top 4 reasons why you would want to create custom exceptions rather than using out-of-the-box exceptions from specific programming languages such as .NET or Java etc:
- Greater Code Trace-ability: As you get into creating enterprise business applications, and you want a way to trace the exceptions with business requirements, functionality and module/components which are serving that functionality, custom exceptions help to a great deal in achieving that objective. It also helps quite a great deal in debugging when you are going throw the logs and found the relevant custom exceptions.Take for instance, the business requirements say that a person would not be able to open his bank account if his age is below a specified number, say, 18 years. And, this is one of key mandatory business rule required for opening an account. Thus, at the time of registration, if the age of the account holder is specified below 18, the code should throw exception mentioning invalid age. There are two ways to handle this:
- Throw the OOTB Exception with message as “Age below allowed minimum limit of 18 years”
- Throw the AccountRegistrationException with message as “Age below allowed minimum limit of 18 years”. With this option, which is custom exception, it achieve dual objective of tracing the business functionality and also the code usability which is described in next point.
The easier way to come up with name of custom exception is to name the exception after business functionality. For example, AccountRegistrationException as mentioned in above example.
- Greater Code Usability: Custom exceptions such as that mentioned in the example above (AccountRegistrationException), also help a great deal in code read-ability and understand-ability. It goes with the flow of code where you name class and method names in relation with functionality and also, the exceptions. This also helps developer to relate the exception with business requirements vis-a-vis exceptions related with them.
- Improved Exception Handling: Custom exceptions help developer determine what exceptions to be thrown as per the business requirement when they are writing the business logic. Thus, it ensures the proper exception handling without a miss if done religiously.
- Ease in Debugging: Custom exceptions also help a great deal in debugging the code. Take an instance where you are going throw the log file and came across the exception such as “AccountRegistrationException“. This would right away alert that the error/exception has occurred with account opening business functionality which is served by account module. The support IT staff could find it easier to locate who to contact in order to have the problem fixed.
Following are some good reads on when to create custom exception with .NET programming.