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 4086096
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:40:38+00:00 2026-05-20T18:40:38+00:00

When handling exceptions in python, I find myself repeating code quite often. The basic

  • 0

When handling exceptions in python, I find myself repeating code quite often. The basic pattern is something of the form:

try:
  action_here()
except CommonException1:
  Action_always_taken_for_CommonException1()
except CommonException2:
  Action_always_taken_for_CommonException2()
except Exception:
  Default_action_always_taken()

What I would like to do is to some how abstract this repetitive code out to a function or class. I know one way to do it is to call an exception handling function with the exception object, such as:

try:
  action_here()
except Exception as e:
  handle_exception(e)

Then in this function determine the exception based on class.

def handle_exception(e):
  if type(e) == type(CommonException1()):
    Action_always_taken_for_CommonException1()
  elif type(e) == type(CommonException2()):
    Action_always_taken_for_CommonException2())
  else:
    Default_action_always_taken()

This, however, feels clunky and inelegant. So my question is, what are some other alternatives to handling repetitive exception handling?

  • 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-20T18:40:39+00:00Added an answer on May 20, 2026 at 6:40 pm

    This situation is one of the main use cases for context managers and the with statement:

    from __future__ import with_statement # Needed in 2.5, but not in 2.6 or later
    
    from contextlib import contextmanager
    
    @contextmanager
    def handle_exceptions():
        try:
            yield # Body of the with statement effectively runs here
        except CommonException1:
            Action_always_taken_for_CommonException1()
        except CommonException2:
            Action_always_taken_for_CommonException2()
        except Exception:
            Default_action_always_taken()
    
    # Used as follows
    with handle_exceptions():
        action_here()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself using python for a lot of file management scripts as the
When I write code in Python with exception handling I can write code like:
I have such code (Python 2.5, GAE dev server): try: yt_service.UpgradeToSessionToken() // this line
Are there any situations when you would use assertion instead of exceptions-handling inside domain
How should exceptions be dispatched so that error handling and diagnostics can be handled
I'm currently working on adding exceptions and exception handling to my OSS application. Exceptions
Hi I have a problem handling exceptions in wcf. I have a service like
Is there a way to centralize the error handling or exceptions handling without using
Question: Is exception handling in Java actually slow? Conventional wisdom, as well as a
For testing exception handling in the case of a broken connection it is useful

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.