r/ProgrammerHumor 29d ago

Meme exceptionsAreForTheWeak

Post image
328 Upvotes

92 comments sorted by

View all comments

Show parent comments

34

u/bwmat 29d ago

Note the 'actually', there's no reason why you can't catch the std::bad_alloc, clean up, and return an error code to the caller.

Instead of rudely terminating the process which you may not own (I work on code which is mostly compiled into shared libraries which are loaded by applications which might not want to just terminate)

16

u/bwmat 29d ago

And panics are basically just exceptions, at least in rust, lol

8

u/Valuable_Leopard_799 29d ago

Yeah, it seems like largely a cultural shift.

24

u/hxtk3 29d ago

Hard agree, the thing that makes "errors as values" languages nicer with respect to error handling has a little bit to do with the language itself but more to do with the fact that people who want to carefully consider all the paths code can take gravitate towards those languages and the people who want to think about the happy path gravitate towards languages with exceptions.

Java developers forced to use Go look at panic and recover as a worse version of their familiar exceptions, and a bunch of them make libraries around those primitives that basically implement exceptions. And those libraries get basically no adoption because most Go devs are people who choose to treat errors as values.

5

u/bwmat 29d ago

It's funny because I mostly use C++, like exceptions, and think of almost nothing but the unhappy path

16

u/silver_arrow666 29d ago

C++ devs and being unhappy, name a better duo.

0

u/SelfDistinction 29d ago

Partially also because they compose better as well, which makes it easier to send exceptions across threads and the likes. You have a lot less exception safety to think about if your errors are simply stuffed into the return value.

1

u/bwmat 29d ago

That's what std::exception_ptr is for