python exit from class after handling exception
python
Solution
Maybe I'm wrong, but I'd keep it simple, using a flag variable and doing this way:
class TestClass(object):
def __init__(self, *args):
self.flag=False
try:
## check some condition
except:
self.flag=True
def do_something_else(self):
if self.flag:
#do what you want, e.g. call a second command
return
...
def return_something(self):
## return something
Problem
I have a class like: ``` class TestClass(object): def __init__(self, *args): try: ## check some condition except: return ## Should exit class def do_something_else(self): ... def return_something(self): ## return something ``` Now I am trying to call the class like: ``` TestClass(arg1, arg2, ..).do_something_else() somthing = TestClass(arg1, arg2, ..).return_something() ``` When I execute the first command, my conditions fails and raise an exception. What I want is that if some exception occurs in `__init__` function then `do_something_method` should not be called and control flow should go to the second command. In the second command, all conditions are met and the `return_something` function should be called. How can I achieve this?