Clear Code
  • Introduction
    • About This Book
    • Timeline
    • Software Killers
    • Billion Dollar Mistakes
    • Clear Code Overview
    • Clear Code Rules
  • Data Processing
    • Separate Data Collection And Processing
    • Create Data Models
    • Separate Use Cases
    • Data Should Be Immutable
  • Examples
    • Separate Use Cases With Data Model
  • Maintenance Cost
    • Consider the Maintenance Cost
    • The Software Exists In Time
    • Don't Feed the Monsters
  • OOP
    • Separate Data And Procedures
    • Do Not Use Inheritance
    • When To Avoid Inheritance?
    • What Is The Problem With Abstract Frameworks?
  • VARIOUS
    • The Real Single Responsibility Principle
    • The problem with Clean Code's name
    • How To Handle Warnings
    • Do Not Create Constant Collection Classes
  • Externals
    • Links
    • Quotes
    • Funny
  • Technology
    • Git Tutorials
  • Clean Code
    • Clean Code Introduction
      • Origin & Overview
      • Advanced
      • Typical Issues
    • Clean Code Outline
      • Why Clean Code?
      • Clean Code
      • Clean Code Approaches
      • Specification & Design
      • Duplication
      • Refinement & Refactoring
      • Conventions
      • Names
      • Types
      • Methods
      • Nulls and Validity Checks
      • Comments
      • Dead Code
      • Error Handling
      • Classes
      • Code Formatting
      • Unit Tests
      • Special Cases
      • Object Oriented Programming
      • General Code Smells
    • Clean Code Links
    • Clean Code TOC
    • Effective Java TOC
Powered by GitBook
On this page
  • Two types of exceptions
  • Good Practices
  • Bad Practices

Was this helpful?

  1. Clean Code
  2. Clean Code Outline

Error Handling

Two types of exceptions

  • Expected or business exceptions ~ checked?

  • Unexpected program or environment errors ~ unchecked?

Good Practices

  • Prefer exceptions to return codes - decreases pollution of clients

  • Prefer exception types to exception payload - error codes

  • Provide context for exception:

    • chain exceptions when wrapping

    • add erroneous object(s)

  • Do not return null - throw exception (e.g. IllegalArgumentException)

  • Prefer runtime exceptions to checked exceptions - decreases pollution and dependency

  • Wrap exceptions in module or service specific exceptions - decrease dependency

  • Only unexpected exceptions should be logged on error level with stack-trace.

Bad Practices

  • Swallow exceptions

  • Implement business logic in catch block

  • Do not catch exceptions unless you handle them

  • handleError() type of methods -

    • not readable what they do

    • they cheat the compiler too

Last updated 5 years ago

Was this helpful?