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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:15:58+00:00 2026-05-24T22:15:58+00:00

I find myself having this sort of pattern over and over: variable = try:

  • 0

I find myself having this sort of pattern over and over:

variable = ""
try:
    variable = ... do some file loading stuff ...
except:
    variable = ""

Is there any way to condense this into a single expression? Like with if-else statements you can turn:

variable = ""
if something:
    variable = somethingelse
else:
    variable = ""

into

variable = somethingelse if something else ""

Is there any equivalent thing for try-catch?

  • 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-24T22:15:59+00:00Added an answer on May 24, 2026 at 10:15 pm

    Since agf already provided the approach I’d recommend, here’s a version of his routine with a couple of minor enhancements:

    def try_except(success, failure, *exceptions):
        try:
            return success()
        except exceptions or Exception:
            return failure() if callable(failure) else failure
    

    This version:

    1. Lets you specify exactly which exceptions will be caught as additional optional arguments. You should always catch the minimum set of exceptions that will do the job and let exceptions you can’t handle bubble up to the caller.

    2. Supports the use of a plain value as well as a function for the failure value. This saves you having to use a lambda in a lot of cases. (Of course, instead of lambda: '' you can just use str.)

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

Sidebar

Related Questions

I find myself having a lot of this in different methods in my code:
I have multiple layers in an application and i find myself having to bubble
Having been spoiled by ruby for so long, I now find myself having to
I'm planning to make a simple Photo Resizer since I usually find myself having
I find myself writing code that looks like this a lot: set<int> affected_items; while
normally I would use Python/Perl for this procedure but I find myself (for political
I've found myself having a requirement to configure log4net based on a file relative
When I write SQL queries, I find myself often thinking that there's no way
I often find myself having to define two versions of a function in order
As part of my current role, I frequently find myself having to work with

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.