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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:54:48+00:00 2026-06-06T08:54:48+00:00

This is some code that is behaving peculiarly. This is a simplified version of

  • 0

This is some code that is behaving peculiarly. This is a simplified version of the behavior that I’ve written. This will still demonstrate the weird behavior and I had some specific questions on why this is occurring.

I’m using Python 2.6.6 on Windows 7.

def demo1():
    try:
        raise RuntimeError,"To Force Issue"
    except:
        return 1
    else:
        return 2
    finally:
        return 3

def demo2():
    try:
        try:
            raise RuntimeError,"To Force Issue"
        except:
            return 1
        else:
            return 2
        finally:
            return 3
    except:
        print 4
    else:
        print 5
    finally:
        print 6

Results:

>>> print demo1()
3
>>> print demo2()
6
3
  • Why is demo one returning 3 instead of 1?
  • Why is demo two printing 6 instead of printing 6 w/ 4 or 5?
  • 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-06T08:54:49+00:00Added an answer on June 6, 2026 at 8:54 am

    Because finally statements are guaranteed to be executed (well, presuming no power outage or anything outside of Python’s control). This means that before the function can return, it must run the finally block, which returns a different value.

    The Python docs state:

    When a return, break or continue statement is executed in the try suite of a try…finally statement, the finally clause is also executed ‘on the way out.’

    The return value of a function is determined by the last return statement executed. Since the finally clause always executes, a return statement executed in the finally clause will always be the last one executed:

    This means that when you try to return, the finally block is called, returning it’s value, rather than the one that you would have had.

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

Sidebar

Related Questions

This is some code that I picked up which I tried to implement. Its
this is some code that SDL requires in visual studios 2005 in order for
This is some simple code that draws to the screen. GLuint vbo; glGenBuffers(1, &vbo);
I have a java code that looks like this: //UI thread //Some code Job
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
I have some code that looks like this: foreach(var obj in collection) { try
I have some code that looks like this: var MyObject = function () {
I have some code that looks like this. There is also an autoincrement field
This is a minimal test case of some code that I actually have. It
I am writing some css code for a school website that looks like this

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.