Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 During handling of the above exception, another exception occurred
#1
i pressed ^C and got:
Output:
Traceback (most recent call last): File "tree.py", line 52, in <module> names=sorted(listdir(path)) NotADirectoryError: [Errno 20] Not a directory: 'projects/avlmap-0.12.4/CONFIG/readsymlink.c' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "xyzzy.py", line 52, in <module> names=sorted(listdir(path)) KeyboardInterrupt
part of the reason for this is that line 52 is in a try: clause, so exceptions are being handled there.

        try:
            names=sorted(listdir(path))
        except:
            names=[]
what if i wanted to specifically handle KeyboardInterrupt in the code that calls this code, and the KeyboardInterrupt happens during the handling of NotADirectoryError, or other kinds of exceptions is cases of other code? will it be able to keep the exceptions apart when there is a handler for each? what if i am doing a recursive call from inside of a try: clause in a recursive function for an asynchronous kind of exception (like KeyboardInterrupt) and two (or more) such exceptions happen together while the recursion is at least two (or more) deep? i am concerned that the nesting of exception events won't always be handled as if they all happened separately.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
This is why you declare which exceptions you are trying to catch explicitly, so that other exceptions can propagate as normal:

try:
    names=sorted(listdir(path))
except NotADirectoryError:
    names=[]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
i do that in most cases. but this is a case where i want any failure of os.listdir() to be handled. so, i guess, your suggestion means i need to research os.listdir() to find out what all it could raise and list them all on the except.

my real concern is nesting of exception handling, and a 2nd exception being raised (of exactly the same type of exception) before the 1st one completes being handled. could it fall back to the previously defined handler or could it handled as a "during" case? how would a "during" case actually be handled in this kind of situation with regard to running code to handle it? i want to be sure that N exceptions result in the handler code running not only N times, but in the correct order. it seems that by default, an internal try trap in effect during except handling (for it to see the cases of another exception during the handling of one).
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#4
The current exception handling was done as PEP 3134. It explains what is going on.

Note that any error coming out of os.listdir() is likely going to be a file error. In 3.x, those are all subclasses of OSError, so you could just try to catch that. It would still be better to target specific errors, though.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
what if, in the future, there are new exceptions added that os.listdir() might produce? i can only imagine that they, too, would be subclasses of OSError. so it might be good to have some, but not too much, breadth of scope, catching those errors. so in this case, catching OSError sounds like a reasonable idea.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#6
Skaperen Wrote:what if i wanted to specifically handle KeyboardInterrupt in the code that calls this code, and the KeyboardInterrupt happens during the handling of NotADirectoryError
It doesn't prevent you from catching KeyboardInterrupt.
import os
import time

try:
    try:
        names = sorted(os.listdir('foo.txt'))
    except NotADirectoryError:
        time.sleep(2)
except KeyboardInterrupt as exc:
    print("Hello, I caught KeyboardInterrupt!")

Quote
#7
but catching different exceptions happens in different for reasons like wat that piece of code was designed for. the different codes are designed for different purposes by different teams.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#8
Quote:catching different exceptions happens in different for reasons like wat that piece of code was designed for
What you want is not clear. I'm saying that having FooError happen during the handling of BarError doesn't remove any possibilities for the code catching FooError. It is a supplement of information which can potentially be exploited by the code.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Receiving XML exception from nmap.scan() results. PythonNmap 4 258 Jan-21-2020, 04:41 AM
Last Post: PythonNmap
  can i raise an exception in a try clause? Skaperen 14 632 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  Continuing code after exception palladium 1 160 Dec-14-2019, 02:43 PM
Last Post: ichabod801
  Handling exception from a module dchi2 11 481 Nov-25-2019, 08:47 AM
Last Post: dchi2
  Pythonn 3 getting the __traceback__attribute of an exception piscvau 1 131 Nov-11-2019, 04:45 PM
Last Post: Gribouillis
  i am getting error while writing code for exception logging rkgupta51179 1 189 Nov-03-2019, 05:12 AM
Last Post: buran
  Py4JJavaError: An error occurred while calling o22720.csv. twinpiques 0 704 Jul-11-2019, 12:41 AM
Last Post: twinpiques
  How to fix 'uncaught exception of type NSException' in Python MonsterPython 0 294 Jul-09-2019, 06:52 AM
Last Post: MonsterPython
  problem using custom exception handling in python srm 3 379 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  'Exception Has occured: UnBoundLocalError' caston 1 349 Jun-12-2019, 02:33 PM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)