Every function that can fail has to tell its caller somehow. Languages have settled on two broad answers, and teams argue about them with more heat than the topic deserves. One answer is the exception: the function stops, the runtime unwinds the call stack, and control jumps to the nearest handler that wants it. The other is the result value: the function returns either a success or an error, and the caller must look at which one it got. Java, Python, C# and JavaScript lean on the first. Go returns an error alongside the normal value. Rust uses a Result type that wraps success or failure. Both styles work. The interesting question is which fits which kind of failure.
What exceptions do well
Exceptions keep the main path readable. Consider a function that reads a file, parses it, and saves rows to a database. Written with exceptions, it reads like the happy path. Any of the three steps may blow up, and the code that knows how to respond sits in one place, perhaps three layers up, where a decision like “show the user an error page” can actually be made. The middle layers do not need to forward errors by hand.
Exceptions are also hard to ignore by accident. An unhandled one stops the program loudly, and the stack trace points at where it happened. For failures nobody planned for, a null reference, a corrupted file, a database that has gone away, that loudness is a feature.
What they do badly
The failure is invisible in the function signature. A method that returns a number might also throw any of six things, and you will only learn that from documentation or from production. Java tried to solve this with checked exceptions, which the compiler forces you to declare or handle. In practice many teams found that it produced a lot of empty catch blocks and wrapper exceptions, and most newer languages did not copy the idea. That is a judgement about how it went, not a law, and some Java teams are happy with it.
Control flow also becomes hard to follow. Any line might jump somewhere else. Resource cleanup needs care: files, locks and transactions must be released on the unhappy path as well, which is why languages added constructs like finally, using and with. And throwing is typically more expensive than returning normally, since the runtime often captures a stack trace. How much more depends on the runtime and version, so measure before you make claims, but it is a reason not to throw in a tight loop for an event that happens all the time.
What result values do well
The possibility of failure is in the type. A function that parses a GST number and returns “valid number or error” tells every caller, at the point of the call, that two outcomes exist. In Rust the compiler will not let you use the value until you have dealt with the error case. In Go, convention puts the error right next to the value so it is hard to miss, though nothing forces you to check it, and unchecked errors are a well-known source of bugs.
Control flow stays local and boring. You can read a function top to bottom and see every exit. Expected failures, such as “that GST number has the wrong checksum”, are just data to be handled, not events.
What they cost
Repetition. Every layer must check and pass the error upward, and the happy path gets interrupted by handling code. Go’s frequent error checks are the standard complaint. Rust reduces the noise with a short operator that returns early on error, but you still carry error types through the program, and combining different error types takes some planning. Context is another issue: an exception comes with a stack trace for free, while a returned error says only what you put in it, so good code adds context as it passes errors up.
My side
Split by how surprising the failure is. If the failure is a normal outcome that the caller is expected to handle, make it a value. A user typing an invalid PAN, a payment declined for insufficient balance, a record not found by ID: these happen daily, they are not bugs, and the code that asks the question can nearly always decide what to do. Dressing them as exceptions turns ordinary business branching into non-local jumps.
If the failure means the program or the request cannot meaningfully continue, such as a broken invariant, an unreachable database or a configuration that is missing, let it be an exception and let it travel to a boundary that logs it and fails the request cleanly. Nobody in the middle can fix it, so forcing each layer to forward it adds only noise.
Where I would argue with the opposite camp: “exceptions for everything” makes validation code slow and surprising, and “values for everything” makes people write error plumbing for failures that no layer will ever handle differently. In a language with only one mechanism you can still follow the split; Python code can return a small result object for expected failures and keep exceptions for the rest, and Go code can use panics only for the truly impossible.
Whichever you choose, two habits matter more than the choice. Never swallow a failure silently, whether that is an empty catch block or an ignored error return. And decide, once, as a team, which kind of failure uses which mechanism, so a reader of the code can tell what to expect.
Leave a Reply