Python Forum
handling 2 exceptions at once
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
handling 2 exceptions at once
#1
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)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PiCamera - print exceptions? korenron 2 792 Dec-15-2022, 10:48 PM
Last Post: Larz60+
Star python exception handling handling .... with traceback mg24 3 1,218 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Class exceptions DPaul 1 1,259 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  is this a good way to catch exceptions? korenron 14 4,593 Jul-05-2021, 06:20 PM
Last Post: hussaind
  Python, exceptions KingKhan248 6 2,945 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 1,981 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  remove spaces with exceptions catosp 4 2,364 May-29-2020, 09:32 AM
Last Post: catosp
  Looking for advice and Guidance on Exceptions used within Functions paul41 1 2,109 Nov-14-2019, 12:33 AM
Last Post: Larz60+
  multi-line messages in raised exceptions? Skaperen 3 7,223 Aug-01-2019, 02:17 AM
Last Post: Skaperen
  Creating custom exceptions that co-operate LadySvetlana 4 3,029 Mar-19-2019, 04:24 PM
Last Post: LadySvetlana

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020