From: Florent R. <f.r...@fr...> - 2015-12-02 21:53:39
|
Hi, Paraic OCeallaigh <par...@gm...> wrote: > I have a fully working script but in the interest of catching something > unexpected, I would like to catch any errors in the script itself and have > it displayed in a infobox or similar, allowing the user to click OK and > continue. > Right now any errors I generate crash the script and are echoed to the > command prompt. This is not desired as the script is a login menu which > should not allow access to the shell. All others methods of getting to the > shell have bee captured except these errors. The normal approach would be something like: d = ... # setup your Dialog instance try: all_the_rest() except: # should be the same as "except BaseException:" d.msgbox(...) This will even catch KeyboardInterrupt (generated when the Python script receives SIGINT, typically when using Ctrl-C---but maybe dialog gets it instead of python in this particular case, this is worth a little check). If you don't want that, you can use "except Exception:" instead of "except:". If you want to print some details about the problem, you could catch the exception with "except BaseException as e:" or "except Exception as e:" and use str(e) or something similar in the message to be printed. Of course, there are some kinds of error conditions that are likely to prevent the script from printing the msgbox (e.g., if someone uninstalls dialog while the script is running; hard disk corruption preventing correct execution of dialog or Python; MemoryError...). It's a bit as if taking "all" precautions not to fall when walking. Should an earthquake start right under your feet, it would be difficult not to fall nevertheless... Regards P.S.: if you really use your script as a login shell, I'm a bit surprised that you could obtain an interactive shell in case the script is terminated by an unhandled exception. I would rather expect you to get back to the login prompt. I assume you ran the program from an interactive shell rather than as a login shell, or that you are just concerned about the traceback being displayed on the terminal in case of an unhandled exception. -- Florent |