Pragmatic Programmer: Using Exceptions for Unexpected Situations

Here we are again with another principle from The Pragmatic Programmer — EXCEPTIONS 📘 One of the problems with exceptions is knowing when to use them. Are they meant for every error? No, They should be used only for unexpected situations — the things that break your assumptions. If something is part of normal program behavior — like: • A user entering invalid input • Searching for a record that might not exist • Checking if an optional file is present That’s normal flow. Handle it with regular conditions (if/else, return values). That’s just your program doing its job. But if something happens that breaks a core assumption — like: • A configuration file that is guaranteed to exist is suddenly missing • A database connection fails unexpectedly • A critical dependency crashes That’s exceptional. That’s when you use an exception. A simple mindset shift helped me: Exceptions are not traffic lights controlling normal flow. They’re fire alarms 🚨 — you use them when something that should never happen actually happens. #programming #softwaredevelopment #cleancode #coding #bestpractices #pragmaticprogrammer #javascript #php #softwareengineering

Thanks Fawzi. Good insights. Also note that raising exceptions has performance overhead

Like
Reply

To view or add a comment, sign in

Explore content categories