r/ProgrammerHumor • • Oct 17 '26

instanceof Trend thatsAFunnyThingThatHappenedOnce

Post image
4.6k Upvotes

524 comments sorted by

View all comments

Show parent comment

8

u/MrHyd3_ Oct 17 '26

What does it do instead of exceptions?

107

u/[deleted] Oct 17 '26

[removed] — view removed comment

40

u/mathisntmathingsad Oct 17 '26

Yeah, the only reason you should try to catch a panic is if you're making something that calls user-provided code unavoidably (i.e. an async runtime)

20

u/shizzy0 Oct 17 '26

Yeah, we catch panics in tests but that’s about it.

16

u/Loading_M_ Oct 18 '26

I work on a library that does catch panics from user code. It's a web framework, and catches panics in user request handlers to return a 500 error to the client. This error is explicitly not overridable (every other default error can be replaced, but not this one), since we don't want you to panic in the first place.

3

u/Cylian91460 Oct 18 '26

or Result, aka "encode the possibility of failure in the return type of the function" aka what the functional programming world has done for ages.

Like Optional in java?

24

u/[deleted] Oct 18 '26

[removed] — view removed comment

2

u/guyblade Oct 18 '26

C++ has a couple of versions of this floating around absl::StatusOr, boost.Outcome, std::expected, and probably others that I'm not aware of.

4

u/[deleted] Oct 18 '26

[removed] — view removed comment

1

u/Hubble-Doe Oct 18 '26

Speak for yourself, I've been working with Java for a few years and think exceptions are pretty neat! they can be passed through calls implicitly without much mental overhead and boilerplate, and when you handle them you still get a nice helpful stacktrace.

3

u/koko775 Oct 18 '26

Result<Foo> ~= Either<Foo,Error>

2

u/Celarion Oct 18 '26

Like monads, with your result boxed as Ok, and any error boxed as Err

2

u/0tter501 Oct 18 '26

Java has added functional programming yes, but Optional is not result, as result stores either the result or the error

optionals store one type of failure value, results store many types of failure values

1

u/shamshuipopo Oct 18 '26

Is Result a monad?

1

u/[deleted] Oct 18 '26

[removed] — view removed comment

2

u/DHermit Oct 18 '26

Could you elaborate for someone who knows Rust, but did Haskell way too many years ago?

2

u/[deleted] Oct 18 '26

[removed] — view removed comment

1

u/DHermit Oct 18 '26

Thank you, that makes sense!

1

u/MrHyd3_ Oct 18 '26

Is it just an if instead of try catch?

→ More replies (1)

-3

u/Mindgapator Oct 17 '26

Same as go if that helps

7

u/Thesaurius Oct 18 '26

Does Go have sum types now? Last time I checked it returned a record and you had to check yourself.

2

u/Mindgapator Oct 18 '26

No indeed, but the paradigm is the same right? Return the error, and deal with it (or forward it) without a try catch. I must say Rust is nicer in that regard but it doesn't change things too much? I don't have enough xp with Rust so might be underestimating it though

→ More replies (2)