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

  • Home
  • SEARCH
  • 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 7526307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:47:37+00:00 2026-05-30T03:47:37+00:00

I find myself handling exceptions without specifying an exception type when I call code

  • 0

I find myself handling exceptions without specifying an exception type when I call code that interacts with system libraries like shutil, http, etc, all of which can throw if the system is in an unexpected state (e.g. a file locked, network unavailable, etc)

try:
    # call something
except:
    print("OK, so it went wrong.")

This bothers me because it also catches SyntaxError and other exceptions based in programmer error, and I’ve seen recommendations to avoid such open-ended exception handlers.

Is there a convention that all runtime errors derive from some common exception base class that I can use here? Or anything that doesn’t involve syntax errors, module import failures, etc.? Even KeyError I would consider a bug, because I tend to use dict.get() if I’m not 100% sure the key will be there.

I’d hate to have to list every single conceivable exception type, especially since I’m calling a lot of supporting code I have no control over.

UPDATE: OK, the answers made me realize I’m asking the wrong question — what I’m really wondering is if there’s a Python convention or explicit recommendation for library writers to use specific base classes for their exceptions, so as to separate them from the more mundane SyntaxError & friends.

Because if there’s a convention for library writers, I, as a library consumer, can make general assumptions about what might be thrown, even if specific cases may vary. Not sure if that makes more sense?

UPDATE AGAIN: Sven’s answer finally led me to understand that instead of giving up and catching everything at the top level, I can handle and refine exceptions at the lower levels, so the top level only needs to worry about the specific exception type from the level below.

Thanks!

  • 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-30T03:47:38+00:00Added an answer on May 30, 2026 at 3:47 am
    1. Always make the try block as small as possible.

    2. Only catch the exceptions you want to handle. Look in the documentation of the functions you are dealing with.

    This ensures that you think about what exceptions may occur, and you think about what to do if they occur. If something happens you never thought about, chances are your exception handling code won’t be able to correctly deal with that case anyway, so it would be better the exception gets propagated.

    You said you’d “hate to have to list every single conceivable exception type”, but usually it’s not that bad. Opening a file? Catch IOError. Dealing with some library code? They often have their own exception hierarchies with a specific top-level exception — just catch this one if you want to catch any of the library-specific exceptions. Be as specific as possible, otherwise errors will pass unnoticed sooner or later.

    As for convention about user-defined exceptions in Python: They usually should be derived from Exception. This is also what most user-defined exceptions in the standard library derive from, so the least you should do is use

    except Exception:
    

    instead of a bare except clause, which also catches KeyboardInterrupt and SystemExit. As you have noted yourself, this would still catch a lot of exceptions you don’t want to catch.

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

Sidebar

Related Questions

I find myself writing code that looks like this a lot: set<int> affected_items; while
When handling exceptions in python, I find myself repeating code quite often. The basic
I find myself frequently writing code like this in the HTML HEAD and other
I find myself writing code like this in ColdFusion a lot: <cfset inputval =
I find myself defining classes like: struct AngleSize { explicit AngleSize(double radians) : size(radians)
I find myself constantly pressing ctrl-z to undo the automatic formatting that happens in
I find myself creating Converters often and would like to be able to: right-click
I find myself having a lot of this in different methods in my code:
Do any languages that support retry constructs in exception handling track and expose the
I find myself working through a lot of other peoples' code nowadays. I noticed

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.