Exception handling / Error Q

Overview

An exception is an unexpected event that occurs in between application flow. When an exception occurs the execution of a program gets terminated. In an organisation like our which deals with lots of citizen data for property, trade license, water and sewerage connection. While processing those applications or during the payment against those applications getting interrupted it will cause huge loss to the end user and the organisation. As there are large programs which are very complex and written by many people, exception handling is one of the important aspects while writing code.

The Exception Handling is one of the effective mechanisms to address the runtime mistakes in order that the ordinary flow of the software can be maintained. Error handling is important because it makes it easier for the end users of your code to use it correctly. Another important issue is that it makes your code easier to maintain. Error handling makes it easier to embed input specifications into the code, so you don't have to look up the design when you write and later maintain the code.Error handling makes debugging easier and it works best if everyone follows the same procedure of error handling while developing the code.

Below are the best practice to be followed:

  1. Use try-catch and final block to handle the exception. This is one of the classic approach.

    1. a try block that encloses the code section which might throw an exception,

    2. one or more catch blocks that handle the exception and

    3. a finally block which gets executed after the try block was successfully executed or a thrown exception was handled.

  2. Prefer to send the error response with specific exception and status code. A coworker who does not know about your code or API which was written by you may need to use that code/API. Therefore make sure to provide as much information as possible. That makes your code/API easier to understand. And as a result, the caller of your method will be able to handle the exception better or avoid it with an additional check. So for example throw a NumberFormatException instead of an IllegalArgumentException. And avoid throwing an unspecific Exception.Refer to the below list for some common response codes to be use while sending error response:

    1. 400 Bad Request – client sent an invalid request, such as lacking required request body or parameter

    2. 401 Unauthorized – client failed to authenticate with the server

    3. 403 Forbidden – client authenticated but does not have permission to access the requested resource

    4. 404 Not Found – the requested resource does not exist

    5. 412 Precondition Failed – one or more conditions in the request header fields evaluated to false

    6. 500 Internal Server Error – a generic error occurred on the server

    7. 503 Service Unavailable – the requested service is not available

  3. Sometimes a status code or error name is not enough to specify what makes the program fail. So it is better to throw exceptions with detailed information.The exception’s message gets read by everyone who has to understand what had happened when the exception was reported. Therefore, describe the problem as precisely as possible and provide the most relevant information to understand the exceptional event.Below are some fields that should be present in the error response body:

    1. Error Code: a unique identifier for the error.

    2. Status: This describes what the error code stands for.

    3. Message: A brief human-readable message.

    4. Description: A lengthier explanation of the error.

  4. Do not ignore the exception. Small enhancements in a service can affect other places. At least write a log message telling everyone that the execution flow just stops at this point and that someone needs to check it.Avoid below code snippet:

    try { // do something } catch (NumberFormatException e) { // this will never happen }

     

  5. Always close the resource connection after use, otherwise it will throw the resource access error when we try to obtain the connection of a particular resource like DB connection.