Exiting a script cleanly without throwing exception
#1
My script runs a series of checks before executing the main command.

If one of the checks is failed I want to exit the script without having to run the rest of them.

If I uses sys.exit() then I get a SytemExit exception and a dialog box telling me there's an error.

What's the correct syntax for exiting a script without generating an exception?
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#2
the script stop itself when finishing.
i don't understand what you need.
Reply
#3
Ok - my fault if I explained badly.

Let's say I've got 4 tests - Test1, Test2 etc

If Test1 is passed then I run Test2, if Test2 is passed then I run Test3 etc

Then, only if all 4 tests are passed, the script runs the commands it needs to.

However, if Test1 is failed, then I don't want to run Test2, Test3 or Test4 and so I want to exit the script at that point. Hence sys.exit()

I appreciate, I can probably manage this in a nested set of "ifs" but I was hoping there'd be an easier route.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#4
implement a variable fail = False

in each test, if test fail then fail = True

if fail = True, don't run the test.

after your tests, if fail = False, then do the rest of the job, if not, do nothing, script ends.
Reply
#5
Yeah, that's what I mean by using "ifs" and I know that will work.

I could probably also run the tests within a "while not fail:" as well.

Many thanks for the quick response, ppic.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#6
no, if is the better way, that's why it exists! , your while will not end if your tests don't fail !
Reply
#7
Well, it's not that it won't end as I could put the final command and a "break" after the tests.

The problem as I just realised is that "while" option would run the whole loop - i.e. as fail would be False at start, it would run all tests, regardless of Test1 being failed.

Will use ifs!
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#8
I know what you mean..while not liked by some I find it useful in e.g. exception handling since it will keep down the "depth" of the code.

I think exiting the whole script is perhaps not the best way, it is better to exit the block and end up in some sort of standard exit clause.

Anyway, have you tested this:
from sys import exit

exit(0) # Successful exit
Reply
#9
vikjon0,

Yes, that's what I'd thought as well - exit 0 in a shell script would indicate a successful exit.

However in Python/XBMC it still registers as an exception.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#10
I've come up against this problem - basically python is built into XBMC and when you running a script/addon it runs it within that process. However, the spawning python code in XBMC is set to catch exceptions.

Unfortunately sys.exit throws an exception, but usually this is unhanded and the script exits - but because XBMC catches it you get an error.

The only way round this was to ensure that the script always ran to natural completion At end of file - which for me meant a large "if" statement around the main code. Very ugly.
Reply

Logout Mark Read Team Forum Stats Members Help
Exiting a script cleanly without throwing exception0