4 ms·
The big question is would the Python script crash or handle the error when obvious problems like not valid JSON or file not found happen? My experience with Sw
by dep_b 10y ago
The big question is would the Python script crash or handle the error when obvious problems like not valid JSON or file not found happen?
My experience with Swift vs Objective-C is that clean Swift is crash free but more verbose when all other things are equal.
If you don't need that level of security because it's just a small script Python was the right choice.
- doubleplusgood 10y agoIt would just raise an OSError/IOError/TypeError/ValueError and exit (unless caught).
- hasenj 10y agoIt would crash but would print an error message along with a stack trace. The rust version will probably just crash with a confusing error.
- wtetzner 10y agoWhy would the Rust version crash? The compiler will warn you if you if you haven't used a result type, and it's up to the programmer to decide what to do in the case of an error, just like when handling an exception. If you were going to use the JSON result for something, then you are forced to check if the result was Ok or Error. The only time you'd get a crash is if you just called .unwrap(), and even then you'll also get a stack trace.
- burntsushi 10y agoNote that you only get a stacktrace if you set the environment variable `RUST_BACKTRACE=1`. Otherwise, you get a standard panic message. The message from `unwrap` is probably unhelpful, which is why a lot of folks advocate using `expect`.
- hasenj 9y agoI think the point of the question was that in Python errors can still happen but they tend to get ignored when writing small scripts. So if one were to explicitly ignore errors in Rust how would the resulting program's behavior differ from the Python version?
- scriptkiddy 10y agoDepending on where it crashed, the Python script would raise an exception. It would most likely be an `IoError`, `KeyError`, or `ValueError`. Then it would show an error message with a line number, column number, and traceback. Using a debugger would allow you to step backwards through the traceback to determine if the error was caused by something further up the line or where the exception was raised. All of Python's exceptions are an instance of `Exception`. In order to catch and handle any exception that can be raised, you can use a `try, except` block with the base `Exception` class. This, however, is bad practice as there may be some exceptions you want to ignore and, generally, you also want to print a different error message depending on which exception was raised.
- dep_b 10y agoSo the Python script proposed in this article really just skips all error checking and will die just the same as the hard unwrapped Rust version with the only benefit it actually produces a more user-friendly error and it doesn't look as ugly. I can't edit my reply anymore but the question was actually meant to be rhetorical rather than I really wanted an answer.
- scriptkiddy 9y ago> So the Python script proposed in this article really just skips all error checking Only if the programmer chooses to not handle exceptions, which is bad practice. >and will die just the same as the hard unwrapped Rust version with the only benefit it actually produces a more user-friendly error and it doesn't look as ugly. No, it won't die unless the programmer wants it to. In Python you can catch an exception and continue the operation in a different manner. In fact, it is common practice in a lot of Python libraries to use exceptions to determine the presence of data and act according to whether or not the exception occurred. If I'm not mistaken, I believe that this is common practice in most languages that utilize the exception pattern. > I can't edit my reply anymore but the question was actually meant to be rhetorical rather than I really wanted an answer. Well, you seem to not know a lot about exception handling in Python, so I hope I was at least a little helpful.
- deleted 9y ago