Hacker News new | ask | show | jobs
by DeathArrow 1341 days ago
It's not like you can't define your own exceptions in C#, so you always could do what the article is saying.

Even if exceptions might be handy, I wouldn't use exceptions for error handling for permance reasons. Instead I have another recommendation: never have void methods and use a Result type that packages the actual value along with an IsValid and Error property. So you will always return Result<T>.

That way, error handling is easy and doesn't cause much performance overhead.

2 comments

> doesn't cause much performance overhead

Exceptions are slow when thrown.

If an error happens rarely, the corresponding exception is thrown rarely, so the performance impact is minimal.

OTOH, if an error can happen frequently, it is no longer "exceptional", and so is probably better handled without exceptions anyway.

I don't think you'd be hitting UnreachableException that much for that to be of performance concern. You actually halt the execution.

And what you are proposing, it's just a different way to handle errors. Maybe appropriate in critical path. However you still have exceptions from .NET framework. OutOfMemoryException, various IOExceptions etc.