-
Notifications
You must be signed in to change notification settings - Fork 197
Scripts: Alter library to enable use of exceptions #1400
Description
Something that's increasingly catching me out is using functions I've provided in the Python libraries in cases where failure of that function is permissible. Already in a couple of functions I've tacked on a boolean argument to specify whether or not to call app.error() if the function can't serve its purpose. I could throw an if not function(): app.error() around every single usage of such functions, but that'd be a lot of unnecessary expansion.
I'm thinking of modifying fundamentally how scripts are executed while making use of the library. Instead of execution proceeding down the main script file, each script would instead define usage() and run() functions, just like the C++ library.
Principally, this would allow placement of a try: except: clause around the run() function, that could catch a tailored MRtrixException. If caught, this would perform similar to what the current app.error() function does. But it would mean that individual library functions could throw an MRtrixException (instead of calling app.error()), which could potentially be caught and dismissed / alter the behaviour of the calling code.
Ideally I'd like to define the code that executes in the case where MRtrixException is not explicitly caught; that would enable both the catching of errors within library functions and the correct cleanup / messaging if not caught, but wouldn't necessitate the other library execution changes. However I haven't been able to find a way to do this.