<jupyter_start><jupyter_text>ExceptionsPython uses special objects called exceptions to manage errors that arise during a program’s execution.Whenever an error occurs that makes Python unsure what to do next, it creates an exception object.If you write code that handles the exception, the program will continue running.If you don’t handle the exception, the program will halt and show a traceback, which includes a report of the exception that was raised.Exceptions are handled with try-except blocks.A try- except  block asks Python to do something, but it also tells Python what to do if an exception is raised. Handling the ZeroDivisionError ExceptionWe’ll tell Python what to do when this kind of exception occurs; that way, if it happens again, we’re prepared.<jupyter_code>print(5/0)<jupyter_output><empty_output><jupyter_text>Using try-except BlocksIf the code in a try block works, Python skips over the except block.If the code in the try block causes an error, Python looks for an except block whose error matches the one that was raised and runs the code in that block.<jupyter_code>try: 
    print(5/0) 
except ZeroDivisionError:
    print("You can't divide by zero!")<jupyter_output>You can't divide by zero!<jupyter_text>Using Exceptions to Prevent CrashesIt’s bad that a program crashed, but it’s even worse to let users see the traceback.Any code that depends on the try  block executing successfully goes in the else  block.<jupyter_code>print("Give me two numbers, and I'll divide them.")
print("Enter 'q' to quit.")

while True:
    first_number = input("\nFirst number: ")
    if first_number == 'q':
        break
    second_number = input("Second number: ")
    try:
        answer = int(first_number) / int(second_number)
    except ZeroDivisionError:
        print("You can't divide by 0!")
    else:
        print(answer)<jupyter_output>Give me two numbers, and I'll divide them.
Enter 'q' to quit.<jupyter_text>Handling the FileNotFoundError Exception<jupyter_code>filename = 'alize.txt'

try:
    with open(filename) as f_obj:
        contents = f_obj.read()
except FileNotFoundError:
    msg = "Sorry, the file " + filename + " does not exist."
    print(msg)<jupyter_output>Sorry, the file alize.txt does not exist.<jupyter_text>Analyzing Text from Multiple FilesLet's try to count the number of words in the text of *Alice in Wonderland*`split()` can build a list of words from a string.The texts used in this section come from Project Gutenberg ( http://gutenberg.org/ )<jupyter_code>def count_words(filename):
    """Count the approximate number of words in a file."""
    try:
        with open(filename, encoding='utf-8') as f_obj:
            contents = f_obj.read() 
    except FileNotFoundError:
        msg = "Sorry, the file " + filename + " does not exist."
        print(msg)
    else:
        # Count approximate number of words in the file.
        words = contents.split()
        num_words = len(words)
        print("The file " + filename + " has about " + str(num_words) + " words.")

filenames = ['alice.txt', 'siddhartha.txt', 'moby_dick.txt', 'little_women.txt']
for filename in filenames:
    count_words(filename)<jupyter_output>The file alice.txt has about 29461 words.
Sorry, the file siddartha.txt does not exist.
The file moby_dick.txt has about 215136 words.
The file little_women.txt has about 189079 words.
StarcoderdataJupyterStructuredCleanDedup