Categories: Software Engg

4 Reasons Why You Would Want to Create Custom Exceptions?

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:
      1. Throw the OOTB Exception with message as “Age below allowed minimum limit of 18 years”
      2. 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.

  1. How to creating custom exceptions in .NET
  2. Custom Exceptions: When should you create them?
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

2 months ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

2 months ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

3 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

3 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

3 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

3 months ago