Python exception ordering
Question: 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
    


Answer: You're printing in the class, not its initialization block . Try running this

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

alone. i.e., it's not running when you call `raise F`. Try this instead

    
    
    import sys
    class F(Exception):
        def __init__():
            sys.stderr.write('Inside exception\n')
            sys.stderr.flush()
    
    raise new F()
    

