Chapter 20 error output doubling

Firstly i want to thank you @lolgrep, this is no doubt the huge book which is gave to me, a kind of noob in iOS development, a huge amount of knowledge about whole low level technology stack, not only iOS debugging.

So back to deal: in the Chapter 20 on the page 262 there is try/except python expression in the code. Here’s that part:

try: #5
  (options, args) = parser.parse_args(command_args)
except:
  result.SetError(parser.usage)
  return

Later you’ve described such try/catch as required due the parsing could be error-prone, so it could crash lldb either debugging application.

So in your code there’s no type of Exception has been provided. On the other hand i’m working with your book in Sublime text editor with anaconda plugin installed and turned on pep8 autocheking. And pep8 requires to set up type of Exception in the code explicitly. So i’ve tried to do so and firstly were failed.

Long story shorts, i’ve to debug with pdb optparse module to understand why the hell every inherited Exception class (including itself) fail to send control flow to the exception line and why the empty “except:” working correct simultaneously.

So the reason is that the optparse module don’t raise the exception in case of wrong option input. It sends sys.exit code in that case.

So this thing have two possible consequences:

  1. There’s no need try/except expression in the bar script, coz the optparse module don’t raise an exception it sends sys exit which shut down main script either. And in case of exception there’s happening double output in lldb console about the option error.
  2. To follow the pep8 there should to explicitly declare BaseException (which as i know now responsible to handle sys.exit’s, thanks to that script) and raise it in the exception expression instead of your script text. Like this, for example.
except BaseException as e:
        raise e

In any case this thing is not a big deal, so posting this i’m firstly want to discuss this point and hear some clever thoughts about it.

Hey @yasroslav,

First off, thank you for the kind words.

In regards to LLDB no longer needing the try, except, you are absolutely correct. This was required back in the day, but since is no longer needed as the authors have resolved this issue of a script tanking lldb. Even though it’s not needed, I am still a fan of using it because it can give me control to perform whatever logic I want based upon bad input. I am happy to add the TypeError exception in future versions to make pep8 happy and change around the error message so you dont get duplicate output errors

Now, in regards to avoid duplicate errors for your own scripts:

  • optparse will print it’s usage by default for an error when it doesn’t recognize an option. You can silence this by doing something like parser.error = None
  • Alternatively in the except condition, you can omit the set SetError method in the instance of SBCommandReturnResult or set it to something different

Cheers