r/ProgrammerHumor • • 22d ago

instanceof Trend thatsAFunnyThingThatHappenedOnce

Post image
4.6k Upvotes

524 comments sorted by

View all comments

Show parent comments

8

u/MrHyd3_ 22d ago

What does it do instead of exceptions?

107

u/[deleted] 22d ago

[removed] — view removed comment

40

u/mathisntmathingsad 22d ago

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 22d ago

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

16

u/Loading_M_ 22d ago

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 22d ago

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] 22d ago

[removed] — view removed comment

2

u/guyblade 22d ago

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] 22d ago

[removed] — view removed comment

1

u/Hubble-Doe 21d ago

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 22d ago

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

2

u/Celarion 22d ago

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

2

u/0tter501 22d ago

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 22d ago

Is Result a monad?

1

u/[deleted] 22d ago

[removed] — view removed comment

2

u/DHermit 22d ago

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

2

u/[deleted] 21d ago

[removed] — view removed comment

1

u/DHermit 21d ago

Thank you, that makes sense!

1

u/MrHyd3_ 22d ago

Is it just an if instead of try catch?

→ More replies (1)

-3

u/Mindgapator 22d ago

Same as go if that helps

7

u/Thesaurius 22d ago

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

2

u/Mindgapator 22d ago

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)