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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:48:38+00:00 2026-05-23T06:48:38+00:00

Just curious, why does the following code import sys class F(Exception): sys.stderr.write(‘Inside exception\n’) sys.stderr.flush()

  • 0

Just curious, why does the following code

import sys
class F(Exception):
    sys.stderr.write('Inside exception\n')
    sys.stderr.flush()
    pass

sys.stderr.write('Before Exception\n')
sys.stderr.flush()

try:
    raise F
except F:
    pass

output:

Inside exception
Before Exception

and not:

Before exception
Inside Exception
  • 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-23T06:48:39+00:00Added an answer on May 23, 2026 at 6:48 am

    As Sven rightly said, class bodies are executed immediately when the class definition is executed.

    In your code, your definition of your class F is above this command sys.stderr.write('Before Exception\n') and hence this sys.stderr.write('Inside exception\n') gets executed first and consequently its output is seen first.

    As a test, run this program –

    import sys
    
    sys.stderr.write('Before Exception\n')
    sys.stderr.flush()
    
    class F(Exception):
        sys.stderr.write('Inside exception\n')
        sys.stderr.flush()
        pass
    
    try:
        raise F
    except F:
        pass
    

    The output now you will get is –

    Before exception
    Inside Exception
    

    This is simply because the statement sys.stderr.write('Before Exception\n') gets executed before the class definition gets executed.

    Infact, the printing of Inside Exception has nothing to do with your initializing an instance of F (by using raise). It will get printed no matter you initialize an instance of F or not.

    Try this –

    import sys
    
    class F(Exception):
        sys.stderr.write('Inside exception\n')
        sys.stderr.flush()
        pass
    

    Still you will get this output –

    Inside Exception
    

    although you have not made an instance of the class F.

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

Sidebar

Related Questions

I'm just curious about this: When evaluating 1/0 in Java, the following exception occurs:
I'm just curious about this really, does anyone know why they broke convention on
im just curious about the following example #include<stdio.h> int test(); int test(){ // int
I am new to SDL, and I am just curious why does sdl use
I am just curious; does any of you have any good javascriptresources for setting
Why does T4MVC generate controller default constructor? Just curious.
Just curious if anybody knows what Ruby technique is used to accomplish the following
Consider the following JavaScript code, meant to create an 8x8 table inside of a
For some reason, the following code does not work: char cValue = '8'; int
Just curious here: is it possible to invoke a Windows Blue Screen of Death

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.