Labs ICT
โญ Pro Login

Error Handling & Exceptions

Designing robust error handling with try/catch and custom exceptions.

Error Handling & Exceptions

Robust error handling is essential for reliable software. Proper exception handling prevents crashes, provides meaningful feedback, and helps developers diagnose problems.

Exception Types


  +---------------------------------------------------------+
  |              EXCEPTION HIERARCHY                         |
  +---------------------------------------------------------+
  |                                                         |
  |   Throwable                                             |
  |   โ”œโ”€โ”€ Error (unrecoverable: OutOfMemory, StackOverflow) |
  |   โ””โ”€โ”€ Exception                                         |
  |       โ”œโ”€โ”€ Checked Exceptions (must handle)              |
  |       โ”‚   โ”œโ”€โ”€ IOException                               |
  |       โ”‚   โ””โ”€โ”€ SQLException                              |
  |       โ””โ”€โ”€ RuntimeException (unchecked)                  |
  |           โ”œโ”€โ”€ NullPointerException                      |
  |           โ”œโ”€โ”€ ArrayIndexOutOfBoundsException            |
  |           โ””โ”€โ”€ IllegalArgumentException                   |
  |                                                         |
  +---------------------------------------------------------+

Try-Catch Best Practices


  // BAD: Swallowing exceptions
  try {
      readFile(path);
  } catch (Exception e) {
      // silently ignore
  }

  // GOOD: Handle and log appropriately
  try {
      readFile(path);
  } catch (FileNotFoundException e) {
      logger.warn("Config file not found, using defaults: " + path);
      return defaultConfig;
  } catch (IOException e) {
      logger.error("Failed to read config file", e);
      throw new ConfigException("Cannot load configuration", e);
  }

Custom Exceptions


  public class InsufficientFundsException extends Exception {
      private final double amount;
      private final double balance;

      public InsufficientFundsException(double amount, double balance) {
          super("Cannot withdraw $" + amount + " with balance $" + balance);
          this.amount = amount;
          this.balance = balance;
      }

      public double getAmount() { return amount; }
      public double getBalance() { return balance; }
  }

Error Handling Principles

  • Don't catch exceptions you can't handle โ€” let them propagate
  • Use specific exception types rather than catching Exception
  • Include meaningful error messages that help diagnose the problem
  • Use try-with-resources to ensure cleanup happens
  • Design your API to minimize the need for callers to catch exceptions

Key Takeaways

  • Never silently swallow exceptions
  • Create custom exceptions for domain-specific errors
  • Distinguish between recoverable and unrecoverable errors
  • Good error handling makes debugging much easier

๐Ÿงช Quick Quiz

Which of the following is an example of error handling best practice?