Try and Catch: Find the Balance
Tags: softwareengineering, codequality, exception
Published: 2023-07-02 04:36:46

## Introduction

Exception handling is a critical aspect of software development, the try-catch mechanism is a powerful tool that allows developers to handle and manage errors effectively. While try-catch blocks are undoubtedly useful, overusing them can lead to significant drawbacks. In this article, we will explore the dangers of overusing try and catch in code and emphasize the importance of finding a balance to ensure robust and maintainable software.

## What is a Try/Catch Block?

In most programming languages, try-catch is a construct that enables developers to handle exceptions or errors that may occur during the execution of code. The try block encloses the code that may potentially throw an exception, while the catch block catches and handles those exceptions, preventing them from causing application crashes or undesirable results.

## The Downsides of Excessive Try and Catch

1. **Error hiding**: Overusing try-catch blocks can lead to a situation where exceptions are caught but not sufficiently handled. This can result in a silent failure, where errors go unnoticed, leading to unexpected consequences. By suppressing exceptions without addressing the underlying issues, developers lose valuable insights into the system’s behavior, making debugging and troubleshooting much more difficult.

```
try {
    // Code that may throw an exception
    $result = myFunction();
} catch (Exception $e) {
    // Logging the exception, but not handling it
    logError($e->getMessage());
}
```

The exception is caught in this example, but it is only logged without taking any appropriate action to handle or address the error. This can lead to hidden bugs or unexpected behavior in the application. Another problem is that the error can hold an unknown message from a runtime exception, without any proper context the readability and searchability will be horrible.

2. **Performance Impact**: Exception handling is generally more expensive in terms of performance compared to regular code execution. Overusing try and catch can introduce unnecessary overhead, impacting the overall efficiency of the software.

Rico Mariani wrote in his article [Exception Cost: When to throw and when not to](https://blogs.msdn.microsoft.com/ricom/2003/12/19/exception-cost-when-to-throw-and-when-not-to/) the first rule:

> When deciding if you should throw an exception, pretend that the throw statement makes the computer beep 3 times, and sleep for 2 seconds. If you still want to throw under those circumstances, go for it.

3. **Clean Code**: An excessive number of try-catch blocks can make code harder to understand. When exceptions are scattered throughout the codebase, it becomes challenging to follow the program flow and understand the logic. Over time, this can create difficulties in maintainability and increase the likelihood of introducing new bugs or inconsistencies while modifying or extending the code.

```
try {
    // Code that may throw an exception
    $result = someFunction();
} catch (Exception $e) {
    // Handling the exception
    // ...additional exception handling code...
}

// More code...

try {
    // Code that may throw another exception
    $result2 = someOtherFunction();
} catch (Exception $e) {
    // Handling the exception
    // ...additional exception handling code...
}
```

4. **Code Smell**: Excessive dependence on try-catch can indicate deeper design issues within the software. When exceptions are used as a primary means of controlling program flow or handling expected conditions, it suggests that the code lacks proper error-handling mechanisms or fails to anticipate and prevent exceptional situations. This not only violates the principles of clean coding but can also create difficulties in future development efforts.

```
try {
    // Code that may throw an exception
    if ($condition) {
        throw new CustomException("Exception occurred!");
    }
} catch (CustomException $e) {
    // Handling the exception but using it for control flow
    // ...additional exception handling code...
}
```

Using exceptions as a primary means of controlling program flow or handling expected conditions suggests a design flaw. In this example, the exception is thrown and caught solely to control the flow of the program, which violates good coding practices and the [Principle of least astonishment](https://en.wikipedia.org/wiki/Principle_of_least_astonishment).

## Finding the balance

1. **Use try-catch for Exceptional cases**: Reserve try-catch blocks for handling exceptional scenarios, where an error is truly unexpected and exceptional. Exceptions should not be treated as a regular control flow mechanism, but rather as a means to gracefully handle unpredicted issues that could cause the application to crash or behave incorrectly.

2. **Avoid Logging and Rethrowing**: While it may seem natural to log an exception when it arises and then rethrow it for the caller to handle, this approach has drawbacks.

```
try {
 // Some code here
} catch (CustomException $e) {
 logError($e);
 throw $e;
}
```

Logging the same exception multiple times creates redundant error messages. Additionally, shifting the responsibility of error handling to the caller forces every consumer of this code to add their own try-catch blocks to handle each rethrown exception.

3. **Single Responsibility Principle**: It’s important to note that while SRP cannot directly help with the multiple uses of try and catch, the use of the Single Responsibility Principle ensures that each function or class focuses on a specific task. By keeping functions concise and focused, you can reduce the need for excessive error handling and make the code easier to understand and maintain.

4. **Logging and Debugging**: Implement a comprehensive logging and debugging strategy to capture and analyze exceptions, enabling effective troubleshooting and error resolution. Proper logging practices allow developers to track down and address issues systematically without relying solely on try-catch blocks.

5. **Handle common conditions without throwing**: If you try to close a connection that’s already closed, you’ll get a StreamCloseException. You can avoid that by using a if statement to check the connection state before trying to close it.

Avoid this:

```
try {
    $stream->close();
} catch (StreamCloseException $e) {
    logError($e);
}
```

By doing this:

```
if ($stream->state !== StreamStateStatus::CLOSE) {
    $stram->close();
}
```

6. **Global exception handler**: If an exception is allowed to bubble up to the global scope, it may be caught by a global exception handler.
while using a global exception handler can be a good practice in many scenarios, the choice between a global handler and multiple try-catch blocks depends on the specific requirements and complexity of your application. It’s important to consider factors such as code simplicity, consistency, maintainability, and the need for fine-grained error handling when making this decision.

```
function exceptions_error_handler($severity, $message, $filename, $lineno) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
}

set_error_handler('exceptions_error_handler');
```

Please notice that there are a few languages that support `Global exception handler` like Node.js for example:

```
process
  .on('unhandledRejection', (reason, promise) => {
    console.error(reason, 'Unhandled Rejection at Promise', promise);
  })
  .on('uncaughtException', err => {
    console.error(err, 'Uncaught Exception thrown');
    process.exit(1);
  });
```

## Conclusion

While try-catch blocks are powerful tools for exception handling in software development, overusing them can lead to significant drawbacks. It is important to strike a balance to ensure robust and maintainable code. By understanding the potential dangers of overusing try and catch, developers can implement best practices to mitigate these risks.