News for hackers. Stuff that matters.
Re:Code Design Decision – Always throw custom exceptions by atst on Monday December 19, 2022 @08:04PM (#34060184)
> The first one I would argue you should wrap third party exceptions. There is in nearly every case important context in your code that the thrower of the third party exception will not know and whoever is reading or handling the exception will want to know.

And this is why exceptions should just not exist. The only ostensible use case is to actually reuse the same few exceptions everywhere so someone can put a generic catch (IOException) that works if you think about your code from a layman perspective, but but also introduces a few vulnerabilities and annoying as hell bugs into the code. But at least it saved 5 lines of code.

When I see some code A -> B -> C where A catches something from C unbeknownst to B, I just assume it's broken. Even in the rare case where I go inspect that library and all 20/20 cases are fine, it is still likely _temporally_ broken code. That is, these kinds of developers will just upgrade dependencies here and there and those dependencies are made by the same kind of developers, and neither communicate with each other about what the interface is, and it just eventually breaks.

If you're doing _anything_ with exceptions other than creating custom exceptions to emulate returning a variant type, your code is broken and results in this:

https://www.lainchan.org/%CE%BB/src/1653782927930-0.jpg

https://www.lainchan.org/%CE%BB/src/1653782927930-1.jpg

https://www.lainchan.org/%CE%BB/src/1653782927930-2.jpg

(this was a huge problem 20 years before Go came out, but Go specifically asked for ridicule here by not having exceptions yet managing to encode the same problem exceptions have, for the sake of solving "problems")

There are no comments on this comment yet.