Python exception handling in list comprehension

后端 未结 3 1818
太阳男子
太阳男子 2020-12-31 09:40

I have a Python function called plot_pdf(f) that might throw an error. I use a list comprehension to iterate over a list of files on this function:



        
相关标签:
3条回答
  • 2020-12-31 10:08

    You could create a catch object

    def catch(error, default, function, *args, **kwargs):
        try: return function(*args, **kwargs)
        except error: return default
    

    Then you can do

    # using None as default value
    result (catch(Exception, None, plot_pdf, f) for f in file_list)  
    

    And then you can do what you want with the result:

    result = list(result)  # turn it into a list
    # or
    result = [n for n in result if n is not None]  # filter out the Nones
    

    Unfortunately this will not be even remotely C speed, see my question here

    0 讨论(0)
  • 2020-12-31 10:10
    try:
        [plot_pdf(f) for f in file_list]  # using list comprehensions
    except:
        print ("Exception: ", sys.exc_info()[0])
        continue
    

    If plot_pdf(f) throws an error during execution of comprehension, then, it is caught in the except clause, other items in comprehension won't be evaluated.

    It is not possible to handle exceptions in a list comprehension, for a list comprehension is an expression containing other expression, nothing more (i.e. no statements, and only statements can catch/ignore/handle exceptions).

    Function calls are expression, and the function bodies can include all the statements you want, so delegating the evaluation of the exception-prone sub-expression to a function, as you've noticed, is one feasible workaround (others, when feasible, are checks on values that might provoke exceptions, as also suggested in other answers).

    More here.

    0 讨论(0)
  • 2020-12-31 10:31

    You're stuck with your for loop unless you handle the error inside plot_pdf or a wrapper.

    def catch_plot_pdf(f):
        try:
            return plot_pdf(f)
        except:
            print("Exception: ", sys.exc_info()[0])
    
    [catch_plot_pdf(f) for f in file_list]
    
    0 讨论(0)
提交回复
热议问题