<jupyter_start><jupyter_text>1. try - Code to be attempted, might lead to error.     2. except - Code executed if there's error in try block.      3. finally - Code to be executed, regardless of the error.<jupyter_code>def add(n1,n2):
    print (n1+n2)
add(10,20)
num1 = 10
num2 = input("Enter a num: ")
add (num1,num2)  # Error since num2 is str not int
print ("This won't get executed!")
try:
    # Want to attempt this code
    # May or may not have an error
    result = 10 + 20
except:
    print ("Not adding correctly!")
result<jupyter_output><empty_output><jupyter_text>Try, Except, Else :<jupyter_code>try:
    # Want to attempt this code
    # May or may not have an error
    result = 10 + 20
    
except:
    # If there is error run except code
    print ("Not adding correctly!")
    
else:
    # If there is no error run else code
    print ("Added correctly!")
    print (result)
    
# If we do "20" then except code will run.<jupyter_output>Added correctly!
30<jupyter_text>Case 1: Everything is fine, gives no error<jupyter_code>try:
    f = open("testfile", "w")
    f.write("Write a test line")
    
except TypeError:
    print ("A type error")
    
except OSError:
    print ("An I/O error")
    
finally:
    print ("I always run")<jupyter_output>I always run<jupyter_text>Case 2: Read instead of write mode, OS error<jupyter_code>try:
    f = open("testfile", "r")
    f.write("Write a test line")
    
except TypeError:
    print ("A type error")
    
except OSError:
    print ("An I/O error")
    
# except:
#   print ("All other exceptions")
    
finally:
    print ("I always run")<jupyter_output>An I/O error
I always run<jupyter_text>Use of 3 in function:<jupyter_code>def ask_for_int():
    
    try:
        result = int(input("Enter a num: "))
    except:
        print ("Provided is not a number")
    finally:
        print ("End of try/except/finally")
ask_for_int()
ask_for_int()<jupyter_output>Enter a num: "10"
Provided is not a number
End of try/except/finally<jupyter_text>Use of 3 in while loop:<jupyter_code>def ask_for_int():
    
    while True:
        try:
            result = int(input("Enter a num: "))
        except:
            print ("Provided is not a number")
            continue
        else:
            print ("Yes ok, that's a number")
            break
        finally:
            print ("I will ask you again \n")
ask_for_int()<jupyter_output>Enter a num: word
Provided is not a number
I will ask you again 

Enter a num: 10
Yes ok, that's a number
I will ask you again
{'fraction_non_alphanumeric': 0.10387045813586097, 'fraction_numerical': 0.015007898894154818, 'mean_word_length': 2.826283987915408, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '76649', 'license': 'MIT', 'n_tokens_mistral': 879, 'n_tokens_neox': 851, 'n_words': 306, 'path': 'Section 10 - Error & Exception Handling/Lec 80 - Error & Exception Handling.ipynb', 'repo_name': 'sansjha4900/Udemy-Python-Notes', 'size': '2532'}