DEV Community

loading...

Discussion on: Error Handling in Rust programming language

Collapse
aminmansuri profile image
hidden_dude

I find that this approach to error handling is a step back.

Back in the C days we had to error check every single line (some even used macros to do it). Because your program can't continue if there's an error. So you do things like:

if(ERROR ==func1()) {
// error handle
}
if(ERROR == func2()) {
// error handle
}

It became messy with so much error handling everywhere. So some "cleaned it up" with GOTO:

if(ERROR==func1())
goto ERROR_BLOCK1;

if(ERROR==func2())
goto ERROR_BLOCK2;

// do stuff

return OK;

ERROR_BLOCK1:
//error
ERROR_BLOCK2:
// error
return ERROR;

So you start seeing a pattern where there is a happy path and an error path in your code. This evolved into the try block and the catch block and led to exceptions where an added benefit is that you don't have to have an error block in every function. (Other languages had an ON ERROR block built in)

It was refreshing to clean up error handling and taking it away from every line of code and isolating it in one place. In essence this is a form of separation of concerns that keeps the code clean.

I fail to see Rust's solution as a step forward. It seems like a regression in language design to me. But I'm happy to learn of any benefits to its approach if I'm missing something.

Collapse
saiumesh profile image
sai umesh Author

I wish I could answer this better but, unfortunately I've never worked with C/C++ so I don't know how they work. now coming to Rust, one can feel like lot of boiler code for handling errors.