October+10th+2016+by+Margaret+Sun

Today we talked bout Chapter 19 **Exceptions**. Then we talked bout three main types of exceptions.  Feature: T he program can be run, but without getting a satisfying answer. This is referred as the most terrible error since it was the algorithm that was being wrong. Example: code format="python" sumU=0 for i in range(10): sumU+=i print(sumU) code  You want to get the sum from 1 to 10, while the code returns 45, which is the sum from 0 to 9-- it is definitely not the result you intended but it can be run. Feature: Errors will be traced back with a message. Example: code format="python" print(1/0) code  You receive a message saying "ZeroDivisionError: integer division or modulo by zero". Feature: Pycharm can highlight it for you to recognize it so it is the easiest to solve. Example: code format="python" prnt("a") code  You receive a message saying "NameError: name ' print ' is not defined", and the only thing you need to do is change "prnt" to the correct name "print". **Use of Try Statement**: It handles the exception. If no exception occurred in the try clause, the except clause will be skipped. If any exception occurred in the try clause, the except clause will be executed and then it is continued.
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">1. **<span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">Exception is an object, and according to the book, whenever it occurs, "the program stops running at this point and Python prints out the traceback".
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">1. Logic error **
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">2. Rum-time error **
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">3. Syntax error **

code format="python" def get_age: age = int(input("Please enter your age: ")) if age < 0: # Create a new instance of an exception my_error = ValueError("{0} is not a valid age".format(age)) raise my_error return age code <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">If the function that called get_age has try statement to handle this, the program continues; otherwise, the error will be traced back. <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">To remind, the ValueError here is a built-in exception in python, instead of a random named came up to your mind. There are other types of exceptions, you can find the list [|here] **.**
 * <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">2. **<span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">To avoid the possible errors made by users to block the program, you might want to create your own exception object and raise it. We used the example from the book:

<span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;">**3.** The Finally clause in try statement is used to clean up the resources we used (or close the file). Whether or not the statement in try is executed, the Finally clause will always be executed. However, only the except clause can be used to handle exceptions.