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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:33:35+00:00 2026-06-17T21:33:35+00:00

I implemented a python class for which the instances are stored in a h5-file.

  • 0

I implemented a python class for which the instances are stored in a h5-file. As such, the __init__()-function of this class first checks if this h5-file yet exists from a previous simulations, and if so raises a raw_input asking to overwrite this file (whereafter the file is overwritten by the new instance) or not, whereafter an error is raised.

I am looking for a way that, when this file (and thus instance) already exists and this is noted in the __init__()-method, i don’t raise an Error whereafter the script stops, buta way to “cancel” the started instantiation and the script just continues … Is there a clean way to implement such “cancel and ocntinue” ?

  • 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-17T21:33:36+00:00Added an answer on June 17, 2026 at 9:33 pm

    The right way to do it is to raise an exception. By default, the exception will propagate out from the __init__ to whoever called it, and eventually all the way to the top, halting the script.

    But if you want to handle that exception and continue, use a try/catch block at whatever level you want to continue from, as described in Handling Exceptions.

    For example:

    class ThingyAlreadyExistsError(RuntimeError):
        pass
    
    class Thingy(object):
        def __init__(self, pathname):
            if os.path.exists(pathname):
                yn = raw_input('{} already exists. Overwrite (y/N)?'.format(pathname))
                if yn.lower != 'y':
                    raise ThingyAlreadyExistsError(pathname)
            # finish initialization
    
    thingies = []
    for pathname in pathnames:
        try:
            thingy = Thingy(pathname)
        except ThingyAlreadyExistsError:
            continue
        thingies.append(thingy)
    

    If you want to catch this before even getting to the __init__, you could always do the check in the __new__ method, or in a @classmethod factory function, or in the for loop, in which case you don’t even need an exception; just don’t initialize. But there’s nothing stopping you from raising an exception inside __init__.

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

Sidebar

Related Questions

I implemented a class in python which currently inherits from dict, but really, I
How is this implemented at a python level? I've got an object that pretends
I have a class in python, which has an iterable as instance variable. I
I want to be able use python descriptors in a class which has the
I have a class method (implemented in a shared object in UNIX environment) which
For my Python 2.7.3 project I have a class called custom_date , which has
In a Python package directory of my own creation, I have an __init__.py file
I have written a class in python that implements __str__(self) but when I use
Lets say that I have only DHT (distributed hash table) implemented (in Python), and
I came across a post in SO where the algorithm is implemented in python

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.