How to deal with a Class generated exception from within Main <p>I'm having some trouble getting my head around how best to solve this situation (mostly due to lack of experience I think). I have written a couple of definitions in a single class that are called from a Main to perform certain tasks. It is assumed the Main could be written by anyone for a number of purposes, so killing the code in the class definition is not preferable. One basic rule of these definitions is, if def1 does its tasks successfully, def2 can be called. But if def1 fails, def2 must not be called as it will fail also.</p> <p>e.g. semi pseudo code, without exceptions:-</p> <pre><code># Test.py if __name__ == '__main__' # variables imported from a file var1 = a var2 = b class.def1(var1, var1) class.def2(var_from_def1) </code></pre> <p>Initially I just performed a sys.exit() in the def1 except:, but as mentioned previously, killing the whole program was undesirable and that it would be preferable to let the exception be thrown and let the Main control whether to call def2. </p> <p>From my understanding of exceptions (as limited as it is) this wouldn't work as the exception operation needs to have already been defined before it is raised. Unless it can be defined in Main, but I'd rather Main has an option to do that.</p> <p>A preferred solution would be for the Main to import the variables, pass them to def1, if def1 failed and Main was not set up to catch the exception the following call to def2 would be cleanly stopped (without killing the process or displaying a fail), no idea how to do that, or if the Main was catching exceptions it could stop the call to def2, load in another set of variables and try def1 again.</p> <p>Here is a conceptual view of the code which may help understand where I'm coming from.</p> <pre><code># Test.py if __name__ == '__main__' # variables imported from file var1 = a var2 = b while new variables to pull from file: try: class.def1(var1, var1) except: print 'def1 exception thrown' else: class.def2(var_from _def1) # class.py class class(object): def def1(self, var1, var2): try: do something potentially flawed except: # this is where I get stuck, not sure how or if I can pass an exception back to the main to decide what to do sys.stdout.write('didn't work') return something </code></pre> <p>Sorry, it does look messy, it's a combination of a few ideas for solutions I had, which probably shouldn't be mixed together.</p> <p>Any suggestions would be great. I have read a few books and forums regarding exceptions but just can't grasp how to best resolve this scenario.</p> <p>Thanks.</p>