How to Catch Exception in youtube-dl in python?
Question: I use youtube-dl in python, sometime I got ContentTooShortError, how can I use
"try...catch..." in python to deal with these exceptions?

I use this code but it doesn't work

    
    
    with youtube_dl.YoutubeDL(options) as ydl:
        # youtube_url = video.youtube_url
        n = 0
        try:
            # 用设置成list的形式
            ydl.download([video.youtube_url])
        except 'ContentTooShortError':
            if n + 1 < max_retey:
                ydl.download([video.youtube_url])
            else:
                return False
    


Answer: Remove the quotes. The exception class need not be added as a string.

    
    
    except ContentTooShortError:
    

And as mentioned by @MichalPawlowski in the comments, make sure you import it.

    
    
    # For Python 3
    from urllib.error import ContentTooShortError
    
    # For Python 2
    from urllib import ContentTooShortError
    

