Python Forum

Full Version: handling 2 exceptions at once
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have mishandling of a case of 2 exceptions at the same time. if each happens one at a time, they are handled OK. but if a second exception happens, i get the trace backs. i want to just gave them both handled.

here is the output i dob't want to get:
Output:
^C Traceback (most recent call last): File "amijsonxz_json_ubuntu_lts_ids.py", line 203, in <module> result=main(cmdpath,args) File "amijsonxz_json_ubuntu_lts_ids.py", line 107, in main amis = json.load(f) File "/usr/lib/python3.6/json/__init__.py", line 299, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "/usr/lib/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/usr/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.6/json/decoder.py", line 355, in raw_decode obj, end = self.scan_once(s, idx) KeyboardInterrupt During handling of the above exception, another exception occurred: Traceback (most recent call last): File "amijsonxz_json_ubuntu_lts_ids.py", line 207, in <module> print(flush=True) BrokenPipeError: [Errno 32] Broken pipe
here is the end of the code where it happens:
if __name__ == '__main__':
    from sys import argv,stderr,stdin,stdout,version_info
    cmdpath = argv.pop(0)
    if argv and argv[0][:2]=='--':
        if '--help' in argv:
            help()
        if '--version' in argv:
            version()
    try:
        result=main(cmdpath,argv)
    except BrokenPipeError:
        exit(141)
    except KeyboardInterrupt:
        print(flush=True)
        exit(98)
    if result is None or result is True:
        exit(0)
    elif result is False:
        exit(1)
    exit(int(float(result)))
i can provide the entire code if you need it (213 lines. 6153 bytes)
You could call your exit() from a finally: block. That will execute before the default handler gets invoked. If your intent is to actually exit the program, that will succeed.

Something like this to show the idea?
import sys
while True:
    try:
        print(3/0)
    except:
        print("I just caught one exception...still going...")
        int("x")
        print("I won't get here...")
    finally:
        # without this finally block, will end in traceback
        print("had some problems.  Exiting")
        sys.exit(5)
And inside the finally block (if you're at least 3.6 I think), then you can examine sys.exc_info(). If it's all None, then the other exception handlers finished. If it's populated, then all the exceptions were not handled.
(Jun-26-2020, 11:02 PM)Skaperen Wrote: [ -> ]i have mishandling of a case of 2 exceptions at the same time.
They are not happening at the same time the second one occurs in the handling of the first.

try:
    raise KeyboardInterrupt
except KeyboardInterrupt:
    raise BrokenPipeError
Error:
Traceback (most recent call last): File "c:/Users/Dave/Documents/VS Code WorkSpaces/pythonforum/general/forumpost2.py", line 2, in <module> raise KeyboardInterrupt KeyboardInterrupt During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:/Users/Dave/Documents/VS Code WorkSpaces/pythonforum/general/forumpost2.py", line 4, in <module> raise BrokenPipeError BrokenPipeError
put a try/except block inside the handler
try:
    raise KeyboardInterrupt
except KeyboardInterrupt:
    try:
        raise BrokenPipeError
    except BrokenPipeError:
        print('caught BrokenPipeError')
Output:
caught BrokenPipeError