Question: There's something weird happening with my code, I have a first function that goes like this :

```
def function1():
    try : #1
        #try to open a file
        #read file
        #return info variable from the file
    except : #1
        try : #2
            #try to open a web page
            #read web page
            if directory1 not in directorylist :
                #create directory1
                #change working directory to directory1
            else :
                #change working directory to directory1
            #write web page content in a file
            #return info variable from the file
        except : #2
            try : #3
                #try to open a second web page
                #print error message 1
            except : #3
                #print error message 2
        #set info variable to None
        #return info variable

```

So this function works perfectly when called in the main program, but when I try to call function1 in another function2, both try#2 and except#2 are executed ! Cause directory1 is created and error message 1 is printed, also my info variable equals None.

How can calling function1 in a second function mess try and except clauses ?

Thank you !

Answer: 
Why is it surprising? `try` block is supposed to execute till some `exception` is raised and after that `except` block will execute. So why does it look like both blocks got executed **in spite of an exception**? 

One of the most likely reasons is there are stuff in `try` block that has nothing to do with the exception being raised. That's the primary reason for the `else` block. Refactoring your code as follows might help

```
try:
    # only statements that might raise exception
except SomeException:
    # except block
else:
    # everything you wanted do if no exception was raised

```

If it's a big chunk of code, fatter the `else` block, things are likely to go smoothly.
If an exception is raised while executing body of try #2 , obviously except #2 will be executed. You probably should check what kind of exception is raised and at which line.