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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:43:39+00:00 2026-06-13T12:43:39+00:00

I am relatively new to Python and I am trying to learn a thing

  • 0

I am relatively new to Python and I am trying to learn a thing or two about classes. I have some experience with functions and I am trying to understand how classes work. My question is about error handling.

Normally with a function, I can perform some operations and if something goes wrong, I can simply return False and validate it where the function’s been called before moving on with the rest of the code execution.

How can I do this with the classes?

I have a simple class with a simple method, say:

my_object = MyObj()
my_object.swim()

When I make my_object “swim”, I want to be able to handle errors or problems, in a simple manner.
For example:

During the execution of def swim(..): I want to be able to return False or raise different errors and handle them without interrupting the application completely.

I.e. I want to:

try:
    my_object.swim()
except:
    # it can not swim. I want it to tell me why or just tell me "False" so I can execute another codeblock and tell it to perhaps .walk_away()

I read about try/except blocks, raising errors, how they are classes. But it does not do what I like, which is to try performing a method and handling the results of that method’s execution (true, false or another error, perhaps?)

How can I do this in Python?

Thank you.

  • 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-13T12:43:40+00:00Added an answer on June 13, 2026 at 12:43 pm

    You would do the following:

    class MayNotSwimException(Exception):
        def __init__(reason):
            self.reason = reason
    
    class Person(object):
        def __init__(self, can_swim):
            self.can_swin = can_swim
    
        def swim(self):
            if self.can_swim:
                self.do_something()
            else:
                raise MayNotSwimException("I can't swim because...!")
    
        def walk_away(self):
            self.do_something_else()
    
    
    
    p = Person(can_swim = False)
    try:
        p.swim():
    except MayNotSwimException as e: # In python 2.5-, use MayNotSwimException, e 
        print "That person can't swim because", e.reason
        p.walk_away()
    

    Exceptions can be used to:

    • Alter the flow of control (jump back to a try/except block)
    • Transfer information through the exception itself. This can be achieved by raising different types of Exceptions (ie. We could define a MayNotSwimBecauseDoesntWantException and a MayNotSwimBecauseFeelsSleepyException and catch them separately) or through attributes of the Exception object itself (e.reason).

    Please have a look at the documentation for a more detailed explanation!

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

Sidebar

Related Questions

So I am relatively new to python, and in order to learn, I have
I am relatively new to Python. I am trying to do some web automation
I am relatively new to python. I have a numpy array that has 3
I am relatively new in Python so facing difficulties in basics. I have following
I'm new to Python (relatively new to programing in general) and I have created
So I'm relatively new to Python and trying to figure out what's the best
I am relatively new to C++ (previous experience in Python and dabbled in Java)
I am relatively new to python. I am trying to copy a directory to
I'm still relatively new to python, 1-2 years of solo-learning, and am trying to
I am relatively new to python (already did some 1h scripts like a little

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.