Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 800243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:15:48+00:00 2026-05-14T23:15:48+00:00

Sorry for the long title, but it seems most descriptive for my question. Basically,

  • 0

Sorry for the long title, but it seems most descriptive for my question.

Basically, I’m having a difficult time finding exception information in the official python documentation. For example, in one program I’m currently writing, I’m using the shutil libary’s move function:

from shutil import move
move('somefile.txt', '/tmp/somefile.txt')

That works fine, as long as I have write access to /tmp/, there is enough diskspace, and if all other requirements are satisfied.

However, when writing generic code, it is often difficult to guarantee those factors, so one usually uses exceptions:

from shutil import move
try:
    move('somefile.txt', '/tmp/somefile.txt')
except:
    print 'Move failed for some reason.'

I’d like to actually catch the appropriate exceptions thrown instead of just catching everything, but I simply can’t find a list of exceptions thrown for most python modules. Is there a way for me to see which exceptions a given function can throw, and why? This way I can make appropriate cases for each exception, eg:

from shutil import move
try:
    move('somefile.txt', '/tmp/somefile.txt')
except PermissionDenied:
    print 'No permission.'
except DestinationDoesNotExist:
    print "/tmp/ doesn't exist"
except NoDiskSpace:
    print 'No diskspace available.'

Answer points go to whoever can either link me to some relevant documentation that I’ve somehow overlooked in the official docs, or provide a sure-fire way to figure out exactly which exceptions are thrown by which functions, and why.

Thanks!

UPDATE: It seems from the answers given that there really isn’t a 100% straight-forward way to figure out which errors are thrown by specific functions. With meta programming, it seems that I can figure out simple cases and list some exceptions, but this is not a particularly useful or convenient method.

I’d like to think that eventually there will be some standard for defining which exceptions are raised by each python function, and that this information will be included in the official documentation. Until then I think I will just allow those exceptions to pass through and error out for my users as it seems like the most sane thing to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:15:49+00:00Added an answer on May 14, 2026 at 11:15 pm

    To amplify Messa, catch what you expect are failure modes that you know how to recover from. Ian Bicking wrote an article that addresses some of the overarching principles as does Eli Bendersky’s note.

    The problem with the sample code is that it is not handling errors, just prettifying them and discarding them. Your code does not “know” what to do with a NameError and there isn’t much it should do other than pass it up, look at Bicking’s re-raise if you feel you must add detail.

    IOError and OSError are reasonably “expectable” for a shutil.move but not necessarily handleable. And the caller of your function wanted it to move a file and may itself break if that “contract” that Eli writes of is broken.

    Catch what you can fix, adorn and re-raise what you expect but can’t fix, and let the caller deal with what you didn’t expect, even if the code that “deals” is seven levels up the stack in main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the long question title. I guess I'm on to a loser on
Sorry if the title is poorly descriptive, but I can't do better right now
Sorry in advance for the long question. What I'm really interested in is a
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for the second newbie question, I'm a developer not a sysadmin so this
Sorry for my ignorance here, but when I hear the word webserver, I immediately
Sorry, I'm new to SVN and I looked around a little for this. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.