Why Error Codes Are Better Than Exceptions
I am a Student, who finds beauty in simple things. I like to teach sometimes.
Search for a command to run...
I am a Student, who finds beauty in simple things. I like to teach sometimes.
No comments yet. Be the first to comment.
There is a distinct heaviness that descends when life proceeds smoothly on the surface. Externally, everything may be stable, yet the desire to die can persist not because of tragedy, but because of a realization regarding the future. If the destinat...
"I must not fear abstraction. Abstraction is the mind-killer. Abstraction is the little-death that brings total obliteration. I will face my abstraction. I will permit it to pass over me and through me. And when it has gone past I will turn the inner...
Ever wondered how the Python or JavaScript code you write actually makes your computer's fans spin up? How do abstract commands like print("Hello, World!") get turned into physical actions? The magic lies in a fundamental, deeply interconnected relat...
I'll speed through setting up an ASIC synthesis flow for the Ibex RISC-V core using entirely open-source tools. Tools Python 3.12.8 (for environment management) Yosys (logic synthesis) sv2v (SystemVerilog to Verilog conversion) OpenSTA (static ti...
Imagine this scene: A dimly lit room, humming with the quiet thrum of advanced technology. Three alien scientists are hunched over a console, staring intently at a string of data flashing across a screen: 0101010100... Alien Scientist #1: "It isn't r...
When writing code, handling errors properly is super important. Two common ways to deal with errors are error codes and exceptions. While exceptions may seem fancy, error codes often work better, especially for performance, readability, and maintainability.
With error codes, you always know what to expect. The function returns a specific number (or an enum) that tells you if something went wrong. You don’t have to worry about unexpected exceptions suddenly stopping your program.
For example:
int result = openFile("data.txt");
if (result == FILE_NOT_FOUND) {
printf("File not found!");
}
Here, we check for the error before doing anything else. No surprises!
Exceptions slow things down. Because handling an exception requires extra processing from the system, like collecting a stack trace and jumping out of the normal code flow. Error codes, on the other hand, are just simple values that the CPU can handle super fast.
If performance is key (like in gaming or embedded systems), error codes are the way to go.
With error codes, you can see all possible outcomes in one place. Exceptions, however, can be thrown from deep inside a function, making it hard to track where the problem started.
Compare these two styles:
int result = connectToServer();
if (result != SUCCESS) {
printf("Connection failed!");
}
try {
connectToServer();
} catch (ConnectionException e) {
printf("Connection failed!");
}
At first glance, both look fine. But what if connectToServer throws multiple exceptions from deep inside the code? Now, you have to track down every possible exception, which can be tricky.
Exceptions jump around the code unexpectedly. If an exception is thrown inside a function and not caught properly, it might crash your entire application.
Error codes keep things simple—you know exactly where errors happen and how they are handled.
Not all programming languages support exceptions, but all of them support returning error codes. If you're working with C, Go, or even low-level systems programming, error codes are the standard.
Exceptions might seem like a convenient way to handle errors, but they introduce unpredictability, performance overhead, and hidden control flow issues. Error codes keep things simple, fast, and easy to debug. These are just my opinions. No need to take it on your organs specially heart and ass. Be chill. No language is superior. Use whatever gets the work done. Peace.