Properly handling exceptions in Python and exit class execution
Question: How to handle inside class execution errors? return something so an outside
function knows that and call the class with other data. What will be the
proper way to do this in Python?

I understand we can handle all exceptions by doing the following:

    
    
    import sys
    try: 
        do_something()
    except Exception:
        do_something_else()
        system.exit('Something happened')
    

and the line "`system.exit('Something happened')`" will exit Python. The thing
is that I have a class, with three methods plus the `__init__()` method. In
total four methods or functions. And I have a function that calls this
**class** with different values and the class performs its actions. I'm not
doing any exception handling in the class because I have no idea how to do so.
Here's what my class looks like.

    
    
    class myClass:
       def __init__(self, values, more values):
          config = []
          answer = self.first_method(values)
          if answer == True:
             self.third_method(values, more_values, answer)
       def first_method(values):
          my_tuple = self.second_method(values)
          return my_tuple
       def second_method(values)
          do_stuff
          return stuff
       def third_method(values, more_values, answer):
          do_stuff...
    

outside function will look like:

    
    
    from my myclass import myClass
    def my_function():
        count = 0
        more_values = [6,7,8,9,10]
        list_of_values = [1,2,3,4,5]
        for values in list_of_values:
            myClass(values, more_values)
            count += 1
    

and what I need is that, if something happens in the `first_method()`,
`second_method()`, and `third_method()`.I will need the class to be stopped
and return something so the outside function will know that the class has been
stopped by something and skip that one and try next one. Did I make myself
clear enough? I don't show the code from my class because I'm still working on
the class itself and also the outside function. But that's the main idea.

Something that might be important: What I'm trying to perform is to create
some images and I'm using PIL. I've been importing the following:

    
    
    from PIL import Image, ImageFont, ImageOps, ImageDraw
    


Answer: Based on my comment about avoiding classes where you don't really need them,
try going about it this way:

    
    
    def start(values, more values):
       config = []
       answer = runfirst(values)
       if answer == True:
          runthird(values, more_values, answer)
    
    def runfirst(values):
       my_tuple = runsecond(values)
       return my_tuple
    
    def runsecond(values):
       do_stuff
       return stuff
    
    def runthird(values, more_values, answer):
       do_stuff...
    

Then, in your "outside" function:

    
    
    from myfunctions import start
    def my_function():
        count = 0
        more_values = [6,7,8,9,10]
        list_of_values = [1,2,3,4,5]
        for values in list_of_values:
            try:
                start(values, more_values)
            except (ValueError, IndexError) as exc:
                print("Houston, we've had a problem:", exc)
                continue
            count += 1
    

No class necessary: all your first, second and third functions are stowed away
neatly in their own module, and don't need a class.

Note: never catch _every_ exception with the generic `except Exception`. Catch
specific exceptions. Because there is always that one exception you overlooked
that you really want to bubble up all the way.

