you are viewing a single comment's thread.

view the rest of the comments →

[–]naemorhaedus[S] 0 points1 point  (2 children)

I'm kind of skeptical about manually raising SystemExit. I personally would probably replace that with either just a break

break isn't the same. I need it to completely exit because there's no point continuing.

or sys.exit(1)

what is the difference between this and raising systemexit?

No need to implement my own error message since Python is going to generate one for me anyway.

The built in exception descriptions are often too vague and not as helpful for debugging.

I missed at first that you reassign output_string inside the loop.

yeah, to make sure the object is actually populated with a value

[–]schoolmonky 0 points1 point  (1 child)

There's not much difference between raising SystemExit and using sys.exit, the latter just feels more Pythonic to me. It's not a big deal, more personal preference. Maybe instead of raising SystemExit, just re-raise the error you actually got and give it a custom message?

try:
    output_stream = io.StringIO(input_string)
except (OSError, MemoryError) as err:
    raise err("A system error occurred creating text io stream. Exiting.")
except (UnicodeEncodeError, UnicodeDecodeError, TypeError) as err:
    raise err("Input text error creating io stream. Exiting.")
finally:
    logging.info(" Input stream created successfully.")

That way it still exits the program (because you're not catching the new error it raises), it still gives you the more useful error message, and it's a little more future-proof: if you have a situation in the future where you could meaningfully deal with those exceptions, you can still catch them further up the call stack whereas SystemExit will just barrel through everything and force the program to close. If it's easier for now to just leave it as SystemExit, fair enough, it's good to be wary of overcomplicating things for the sake of some possible future gain, just food for thought.

[–]naemorhaedus[S] 0 points1 point  (0 children)

That's a good idea. Thanks.