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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:53:35+00:00 2026-05-31T11:53:35+00:00

Many try/except/finally-clauses not only uglify my code, but i find myself often using identical

  • 0

Many try/except/finally-clauses not only “uglify” my code, but i find myself often using identical exception-handling for similar tasks. So i was considering reducing redundancy by “outsourcing” them to a … decorator.

Because i was sure not to be the 1st one to come to this conclusion, I googled and found this – imho – ingenious recipe which added the possibility to handle more than one exception.

But i was surprised why this doesn’t seem to be a wide known and used practice per se, so i was wondering if there is maybe an aspect i wasn’t considering?

  1. Is it bogus to use the decorator pattern for exception-handling or did i just miss it the whole time? Please enlighten me! What are the pitfalls?

  2. Is there maybe even a package/module out there which supports the creation of such exception-handling in a reasonable way?

  • 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-31T11:53:37+00:00Added an answer on May 31, 2026 at 11:53 am

    The biggest reason to keep the try/except/finally blocks in the code itself is that error recovery is usually an integral part of the function.

    For example, if we had our own int() function:

    def MyInt(text):
        return int(text)
    

    What should we do if text cannot be converted? Return 0? Return None?

    If you have many simple cases then I can see a simple decorator being useful, but I think the recipe you linked to tries to do too much: it allows a different function to be activated for each possible exception–in cases such as those (several different exceptions, several different code paths) I would recommend a dedicated wrapper function.

    Here’s my take on a simple decorator approach:

    class ConvertExceptions(object):
    
        func = None
    
        def __init__(self, exceptions, replacement=None):
            self.exceptions = exceptions
            self.replacement = replacement
    
        def __call__(self, *args, **kwargs):
            if self.func is None:
                self.func = args[0]
                return self
            try:
                return self.func(*args, **kwargs)
            except self.exceptions:
                return self.replacement
    

    and sample usage:

    @ConvertExceptions(ValueError, 0)
    def my_int(value):
        return int(value)
    
    print my_int('34')      # prints 34
    print my_int('one')     # prints 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this code, many keys are output, but I expected no output: import os
I have used try-catch/except-finally variants in many languages for years, today someone asked me
Many times we find ourselves working on a problem, only to figure out the
Long story coming up, but I'll try to keep it brief. I have many
I find many examples on various gtk uses on the net but most of
This is my first time using StackOverflow myself. I have found many answers to
I know many of these questions exist but I can't seem to find one
This article on MSDN states that you can use as many try catch blocks
While we try to set up as many unit tests as time allows for
I try to realize a link (in fact many links) that update a table

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.