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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:53:12+00:00 2026-05-30T23:53:12+00:00

I am learning from the book Python for Absolute Beginners and am up to

  • 0

I am learning from the book Python for Absolute Beginners and am up to the chapter on exceptions. The authors explanations have been increasingly short, and with this piece of code, i am completely confused and there is no explanation! Can someone explain please, line by line?

#!/usr/bin/env python
store = []
try: {}["foo"]
except KeyError as e: store.append(e)
try: 1/0
except ZeroDivisionError as e: store.append(e)
try: "".bar()
except AttributeError as e: store.append(e)
for exceptionobject in store:
    ec = exceptionobject.__class__
    print(ec.__name__)
    indent = " +-"
    while ec.__bases__:
        ec = ec.__bases__[0]
        print(indent + ec.__name__)
        indent = " " + indent
  • 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-30T23:53:13+00:00Added an answer on May 30, 2026 at 11:53 pm
    # create a list
    store = [] 
    
    try:
        # if something in this block would throw an exception,
        # code could continue in a controlled way in an except block
    
        # force a KeyError by looking up a non-existent key in an empty dictionary
        {}["foo"] 
    
    except KeyError as e: 
        # store the exception object in the list
        store.append(e)
    
    # same scheme here; construct something that fails (1/0), 
    # then instead of quitting the interpreter, continue operations
    try: 
        1/0
    except ZeroDivisionError as e: 
        store.append(e)
    
    # pythons exceptions hierarchy is diverse and exceptions carry meaningful
    # names expressing their context
    try:
        # here we attept to lookup an attribute (that's happening technically
        # before the call), which does not exists (because strings don't know
        # how to bar... 
        "".bar()
    
    except AttributeError as e:
        # ... and the the appropriate exception here is AttributeError
        store.append(e)
    

    At this point the list has three elements, which are exception objects.

    # loop over list
    for exceptionobject in store:
    
        # get the class of the object via special method __class__
        # __class__ returns an object whose class is type actually;
        # but don't be too confused by this 
        ec = exceptionobject.__class__
    
        # print the name of the exception class, now this is just a plain string
        print(ec.__name__)
    
        indent = " +-"
    
        # use another special method __bases__ to get all superclasses
        # of the exception class; all exceptions inherit from BaseException
        # loop over the base classes
        while ec.__bases__:
    
            # get the first base class
            ec = ec.__bases__[0]
    
            # print its name an indent more
            print(indent + ec.__name__)
            indent = " " + indent
    

    The result should look something like this:

    KeyError
     +-LookupError
      +-StandardError
       +-Exception
        +-BaseException
         +-object
    ZeroDivisionError
     +-ArithmeticError
      +-StandardError
       +-Exception
        +-BaseException
         +-object
    AttributeError
     +-StandardError
      +-Exception
       +-BaseException
        +-object
    

    showing a part of the exception hierarchy.

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

Sidebar

Related Questions

I'm currently learning python from a book called 'Python for the absolute beginner (third
I'm learning Python from a book, and I came across this example: M =
I'm learning Python from a book and came across this example: >>> '%f, %.2f,
in the book i'm learning from i came across this code snippit: while (i
I am learning JPA from this tutorial . I have some confusions in understanding
I have been in at the deep end for a week now learning from
I am learning ruby from this guide http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:deploying , and I am trying to
I am learning Python, following a book (Python Programming for the Absolute Beginner, 3rd
I recently acquired this book from Microsoft Press. I currently have Office Enterprise 2007
A) Book I’m learning from says that if we handle Login.Authenticate event, then we

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.