14

Trying to get the try/except statement working but having problems. This code will take a txt file and copy the file that is in location row 0 to location of row 1. It works however if i change one of the paths to invalid one it generates an error ftplib.error_perm however the except command is not picking up and everything stops. What am i doing wrong? Python 2.4

import csv
import operator
import sys
import os
import shutil
import logging
import ftplib
import tldftp

def docopy(filename):
        ftp = tldftp.dev()
        inf = csv.reader(open(filename,'r'))
        sortedlist = sorted(inf, key=operator.itemgetter(2), reverse=True)
        for row in sortedlist:
                src = row[0]
                dst = row[1]
                tldftp.textXfer(ftp, "RETR " + src, dst)


def hmm(haha):
    result = docopy(haha);
    try:
        it = iter(result)
    except ftplib.error_perm:
        print "Error Getting File" 


if __name__ == "__main__":
        c = sys.argv[1]
        if (c == ''):
                raise Exception, "missing first parameter - row"
        hmm(c)
1
  • 3
    Aside: that's some weird looking indentation, and that's going to lead to problems. Run your code with python -tt your_program_name.py to confirm the inconsistent whitespace use, and then switch to 4-space indentation everywhere. Commented Jan 22, 2013 at 13:57

8 Answers 8

11

The except clause will only catch exceptions that are raised inside of their corresponding try block. Try putting the docopy function call inside of the try block as well:

def hmm(haha):
    try:
        result = docopy(haha)
        it = iter(result)
    except ftplib.error_perm:
        print "Error Getting File" 
Sign up to request clarification or add additional context in comments.

2 Comments

That made it so it printed the "error getting file", however shouldn't the try command then pick back up and continue with the rest of the result?
@user1943219 Nope, once there is an error in the try block execution of code in the try block is done and will not start again.
4

The point in the code which raises the error must be inside the try block. In this case, it's likely that the error is raised inside the docopy function, but that isn't enclosed in a try block.

Note that docopy returns None. As such, you will raise an exception when you try to make an iter out of None -- but it won't be a ftplib.error_perm exception, it'll be a TypeError

Comments

1

If you are not sure of what exception will occur, the use the code below, because if especifies for example: except StandardError: and is not that error the exception will not be process.

try:
    # some code
except Exception: # Or only except:
   print "Error" # Python 3: print("Error")

Comments

0

I know the OP is ancient, but for folks desperate for answers on this question. I had a similar issue, depending on your IDE, if you have a breakpoint on any of the lines with specific exceptions etc, this can conflict and stop try/except executing.

Comments

0

I noticed global exception may not works, e.g. , Ctrl+C when epub.py module perform urllib3 connection trigger KeyboardInterrupt but not able to catch in main thread, the workaround is put my clean up code inside finally, e.g.:

try:
    main()
except Exception as e:
    clean_up_stuff()  #this one never called if keyboard interrupt in module urllib3 thread
finally: #but this work
    clean_up_stuff() 

Comments

0

This example is generic for Python3.3+, when decorating a generator function, a decorated generator returns successfully, thus not entering the decorators except, the magic happens with yield from f thus wrapping the yieldable within the decorator:

from types import GeneratorType    

def generic_exception_catcher(some_kwarg: int = 3):
    def catch_errors(func):
        def func_wrapper(*args, **kwargs):
            try:
                f = func(*args, **kwargs)
                if type(f) == GeneratorType:
                    yield from f
                else:
                    return f
            except Exception as e:
                raise e
        return func_wrapper
    return catch_errors

Usage:

@generic_exception_catcher(some_kwarg=4)
def test_gen():
    for x in range(0, 10):
        raise Exception('uhoh')
        yield x

for y in test_gen():
    print('should catch in the decorator')

Comments

0

I ran into a weird corner case that caused this problem and that might be worth sharing.

I'd refactored a custom error class into a new package, and there was a place in the code where it was being thrown, but the raise call referenced the old package in a local import statement. For some reason, the exception was constructed and raised without being caught by except Exception: (which I added as a sanity check when I couldn't figure out what was going on.)

The solution of course was to fix the name of the package in the place where the error was imported. I'm guessing my virtual environment or cache is polluted with the old error package and class, which is why it was imported and constructed successfully. However, I'm very surprised I couldn't catch it at all, since even in the old package it inherited from BaseException.

Note: this occurred when I was executing the code in a Python 3.11 runtime.

Comments

0

Are you running your code using the VSCode terminal window?

python3 my_script.py myparam

If so -- you may find that a try-exception block doesn't reliably catch exceptions.

Consider this code:

parser = argparse.ArgumentParser("file_reader.py")
parser.add_argument("filepath", help="file to open.", type=str)
args = parser.parse_args()
if args.filepath:
    text_file = Path(args.filepath) 
    try:
        contents = text_file.read_text()
        lines = contents.splitlines()
        line_count = 0
        for line in lines:
            line_count += 1
            print(f"{line_count}.\t{line}")
    except Exception:
        print(f"The file: {args.filepath} could not be opened.\n")

Testing this code in a vscode terminal, and passing a non-existant filepath generated an error, despite the try-exception block.

Eventually I discovered that this was being cause by vscode, and the exception was caught properly when I ran it from a terminal session.

You have a couple of options:

  • In vscode, Exit all open terminals and then re-open the terminal. This might fix the issue.
  • Utilize a separate terminal program to run the script.

In summary, there are some things that don't always work as expected when using the vscode terminal. If an exception is not being caught, and displays an error message and error code instead, try the code from an independent terminal to verify that the problem isn't the vscode terminal.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.