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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:46:26+00:00 2026-06-03T13:46:26+00:00

In Python, how can I use except block with same exception name twice in

  • 0

In Python, how can I use except block with same exception name twice in try/except statements without need to wrap code into one more try/except block?

Simple example (here each call of pages.get may raise the exception):

try:
    page = pages.get(lang=lang)
except Page.DoesNotExist:
    if not lang == default_lang:
        page = pages.get(lang=default_lang)
    else:
        raise Page.DoesNotExist
except Page.DoesNotExist:
    page = pages[0]

For now, in my Django app I do handling like this (but I don’t want “extra” try block here):

try:
    try:
        page = pages.get(lang=lang)
    except Page.DoesNotExist:
        if not lang == default_lang:
            page = pages.get(lang=default_lang)
        else:
            raise Page.DoesNotExist
except Page.DoesNotExist:
    page = pages[0]

Any handling code better than above is appreciated! 🙂

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-06-03T13:46:26+00:00Added an answer on June 3, 2026 at 1:46 pm

    You can’t do this either and expect the elif to execute:

    if foo == bar:
      # do "if"
    elif foo == bar:
      # do "elif"
    

    And there’s no reason to do this, really. Same for your except concern.

    Here’s the disassembled Python bytecode of your first code snippet:

     13           0 SETUP_EXCEPT            10 (to 13)
    
     14           3 LOAD_GLOBAL              0 (NameError)
                  6 RAISE_VARARGS            1
                  9 POP_BLOCK           
                 10 JUMP_FORWARD            44 (to 57)
    
     15     >>   13 DUP_TOP             
                 14 LOAD_GLOBAL              0 (NameError)
                 17 COMPARE_OP              10 (exception match)
                 20 POP_JUMP_IF_FALSE       35
                 23 POP_TOP             
                 24 POP_TOP             
                 25 POP_TOP             
    
     16          26 LOAD_GLOBAL              0 (NameError)
                 29 RAISE_VARARGS            1
                 32 JUMP_FORWARD            22 (to 57)
    
     17     >>   35 DUP_TOP             
                 36 LOAD_GLOBAL              0 (NameError)
                 39 COMPARE_OP              10 (exception match)
                 42 POP_JUMP_IF_FALSE       56
                 45 POP_TOP             
                 46 POP_TOP             
                 47 POP_TOP             
    
     18          48 LOAD_CONST               1 (1)
                 51 PRINT_ITEM          
                 52 PRINT_NEWLINE       
                 53 JUMP_FORWARD             1 (to 57)
            >>   56 END_FINALLY         
            >>   57 LOAD_CONST               0 (None)
                 60 RETURN_VALUE        
    

    It’s obvious that the first COMPARE_OP to NameError (at offset 17) will catch the exception and jump to after the second such comparison (at offset 36).

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

Sidebar

Related Questions

In Python I can use get method to get value from an dictionary without
Can I use type as a name for a python function argument? def fun(name,
I have the following python code: try: pr.update() except ConfigurationException as e: returnString=e.line+' '+e.errormsg
In python 2.71 on Windows XP I need to use FTP. My code is
Consider this try / except block I use for checking error message stored in
In python we can use multiprocessing modules. If there is a similar library in
In Python I can use the iterkeys() method to iterate over the keys of
In python, is there a way I can use instance variables as optional arguments
I'm planning to use very big numbers in Python, but wonder if Python can
On Google App Engine to query the data store with Python, one can use

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.