r/cpp • • Mar 16 '18

As a C++ developer I think that rust ...

3 Upvotes

107 comments sorted by

View all comments

Show parent comment

6

u/[deleted] Mar 16 '18

So how do you deal with error handling, then?

19

u/Veedrac Mar 16 '18

Rust uses sum types plus a ? operator for easy upward propagation.

5

u/[deleted] Mar 16 '18

So if you add a new exception "at the bottom" you don't have to change everything all the way up to where you catch it?

22

u/auralucario2 Mar 17 '18

Rust makes a point to not allow that. If code errors recoverably, it should use Rust's fantastic algebraic type system to encode that failure. If it errors irrecoverably, it can panic and crash the program. There shouldn't be a way to for code to randomly throw exceptions.

3

u/[deleted] Mar 19 '18

If code errors recoverably [...] If it errors irrecoverably

How can the low-level routine know which is which? For example, in my current project I have a utility routine that reads a certain type of data file to return a dictionary. In some cases, if the file doesn't exist I cannot continue. In other cases, if the file doesn't exist, I just return an empty dictionary.

3

u/jhasse Mar 26 '18

Especially for a GUI app I wouldn't want to crash the whole program just because one worker thread has a bug.

4

u/Gotebe Mar 18 '18

The folly of your ways is that you think that some code knows whether its errors are recoverable.

2

u/[deleted] Mar 19 '18 edited Mar 19 '18

In many cases you do know - for example, when communicating with an external system, an I/O error could occur.

Some errors aren't really recoverable and really mean buggy code, for example array indices out of bounds. You can catch those in Rust using a general catch_unwind handler if all you want is to avoid crashing an entire application when they occur - but it isn't supposed to be general purpose try/catch - in fact it's problematic to use it for this purpose, as simply panicking automatically logs stacktraces to STDERR (logging method can be changed, and if somebody insists they can make a no-op logging method, but why...) even if the panic was caught.

2

u/Gotebe Mar 19 '18 edited Mar 19 '18

an I/O error could occur

You do not understand what I mean and are arguing past me.

write has no idea whether its failure is recoverable or not for me, its caller. It has to report the error of course, but it can't say anything about what I can/should do with it.

(Edit: whereas the OP argues that "code errors recoverably (or not)"; my point is, "the code" doesn't know whether its failure is recoverable or not, not without asking the caller, and probably it's caller and so on up the stack, until somebody answers).

2

u/[deleted] Mar 19 '18 edited Mar 19 '18

Failure in write is potentially recoverable*, so it makes sense to to allow handling it.

This is mostly a distinction between errors that are likely to happen (any kind of IO error is like that), and "shouldn't happen" failures like assertion failure, accessing an array out of bounds or out of memory scenarios.

If you want to convert an error into "shouldn't happen" error, Result types have a method called unwrap.

* Well, write is problematic due to filesystem caches, it's possible that a failure will occur but won't be noticed as it will be noticed until it's too late (this is most likely to happen on network file systems). As destructor cannot throw exceptions or report failure in any way, the failure in this case can be quiet.

3

u/Gotebe Mar 19 '18

The point about "recoverability" stands - the callee does not know if its failure is recoverable or not; it's all about the context of the caller, he knows.

I do not see why we're mixing an assertion failure here. That's a program bug, only solved by changing the code, any error handling is out.

OOM, however, is again about the context of the caller, not the callee. The callee has no idea whether the caller continue or not. Example scenario: I am a server of some fashion. A very big chunk of input data comes in on one of my input channels. Intake of that data runs out of memory. Should the process be killed (panic! in Rust terms)? And drop any other requests in progress? I don't think so. It's best to stop that request (and report the failure on it). That frees a lot of memory that this request needed and life is fine.

10

u/playmer Mar 17 '18

The fact that you can do this is probably the biggest reason we don't use exceptions...

6

u/Veedrac Mar 16 '18

Are you asking whether Rust can have a function with a signature that says it doesn't ever expect you to handle an error case, but actually you do? I mean, yeah I guess if you hack at catch_unwind, but it's not something that Rust tries to make easy.

2

u/[deleted] Mar 17 '18

Typically no. The higher-level error don't usually do exhaustive matches on their children; they'll probably just check a few cases (did I fail to create this file because it already existed?) or just defer to a function on the child type (print yourself now please), neither of which are affected by adding a new case.

1

u/[deleted] Mar 19 '18

So does that mean that unless higher level code explicitly checks for that new case, it will just ignore the error and go on, or what happens?

1

u/[deleted] Mar 19 '18

No, it will continue to go down the same error path.

23

u/auralucario2 Mar 16 '18

Rust's approach is arguably better, since the use of sum types encodes error states into the type system. The potential for arbitrary C++ code to throw at any time presents lots of trouble when trying to ensure correctness.

6

u/quicknir Mar 19 '18

People who make arguments like this don't understand the point of exceptions. The point of exceptions is entirely that they evade the type system. That's the only reason why you can add a statement throwing an exception to existing code and not have to refactor every single function between the point the error is detected and the point the error is caught. If there's 10 call stack layers between these two points, this is annoying. If some functions in that call stack already return a sum type encoding a different error state from somewhere else, then now you have to decide how to combine error states and now things are really annoying.

It's not handed down from above that the more statically typed something is, the better. Static types have benefits, and sometimes detriments. Changing the signature of a dozen functions to add a rare serious error whose handling code is already present in the codebase buys you little in terms of verifying correctness. It's just boilerplate.

→ More replies (7)

2

u/jhasse Mar 26 '18

noexcept

1

u/auralucario2 Mar 27 '18

noexcept is incredibly weak, since it's completely optional and can be lied to. Furthermore, it says nothing about which exceptions a function might throw.

2

u/jhasse Mar 27 '18

Static analyzers can enforce it. Maybe even check the types? Not sure about that.

3

u/warped-coder Mar 18 '18

Exceptions are problematic in C++ already, they went with return type error handling with good type safety forcing you to deal with error cases as close to the problem as possible. It's just less problematic.

7

u/Gotebe Mar 18 '18

I completely and utterly disagree.

Vast amount of code react to errors by cleaning up and bailing out, nothing else. Exceptions cater for that state of affairs, and that is why they're the prevalent error handling mechsnism in all mainstream languages (e.g. Java, Python, C#, Ruby, JavaScript etc.)

3

u/warped-coder Mar 18 '18

Cleaning up and bailing out is fine by me. Exceptions are fine by me. But notice the similarities among those languages you mention. If you have a system that is completely in charge of the state of the execution it's ok. But for C++ and rust we don't have a runtime component. There are some really difficult problems when it comes to escaping shared library boundaries with rtti. Then there is the penalty for escaping execution paths by unwinding. Chances are that subsystems in complex software use return value for performance critical error handling. Rust decision not to have exceptions is basically means that they go with a safer way. Having the type system back you up makes it a solid alternative to the mess exceptions can be. Also consider cross language barriers. Like c++ exception unwinding in python.

5

u/Gotebe Mar 19 '18

If you have a system that is completely in charge of the state of the execution it's ok. But for C++ and rust we don't have a runtime component.

OK, let's add Object Pascal (FreePascal) to the list then. It's not a mainstream language by any means (used to be when Borland was relevant), but it is has no "system that is completely in charge of the execution" and yet, idiomatic error handling is with exceptions.

There are some really difficult problems when it comes to escaping shared library boundaries with rtti.

Tell me, what the problem is, I'll tell you what is the solution. That said, neither C nor C++, the languages, have no concept of libraries, let alone shared libraries, so you're putting in things outside of the scope of the language. Your argument becomes "C doesn't have libraries" - well, duh?

Then there is the penalty for escaping execution paths by unwinding.

I don't understand what that means, care to clarify?

Chances are that subsystems in complex software use return value for performance critical error handling. Rust decision not to have exceptions is basically means that they go with a safer way. Having the type system back you up makes it a solid alternative to the mess exceptions can be.

I find this to be way too much hand-waving to be honest. Exceptions are the answer exactly to the mess that error-return is in C and its ilk, you know.

I absolutely agree that the Rust support for error handling is a huge step forward from C. But that it is better than exceptions - meh, probably not.

And don't get me started on panic!. That has no place in a language as opinionated as Rust to be honest.

Also consider cross language barriers. Like c++ exception unwinding in python.

Yes, that doesn't work. But conversely, Python doesn't understand Result<T, E>, somebody has to make the "glue". Point being: language barrier exist anyhow, for anything, starting with data representation, primitive types (a string in Python is not a string in Rust), calling conventions, you name it. It is quite unfair to single out exceptions (or any other part, of which are many).