Python Exception Handling Damian Gordon Exception Handling When

  • Slides: 27
Download presentation
Python: Exception Handling Damian Gordon

Python: Exception Handling Damian Gordon

Exception Handling • When an error occurs in a program that causes the program

Exception Handling • When an error occurs in a program that causes the program to crash, we call that an “exception” (since something exceptional has occurred). • We say that “Python raises an exception” when an error occurs.

Exception Handling • What happens if we try to open a file that doesn’t

Exception Handling • What happens if we try to open a file that doesn’t exist?

Exception Handling • What happens if we try to open a file that doesn’t

Exception Handling • What happens if we try to open a file that doesn’t exist? >>> Traceback (most recent call last): File "C: Python 34File. Read. py", line 3, in <module> file_pointer = open("C: Python 34My. Dtaa. txt", "r") File. Not. Found. Error: [Errno 2] No such file or directory: 'C: \Python 34\My. Dtaa. txt'

Exception Handling • Python has a way of intercepting the exceptions (and handling it)

Exception Handling • Python has a way of intercepting the exceptions (and handling it) before the program crashes, and exiting gracefully. • Using the try and except commands.

Exception Handling # PROGRAM Exception. Handing 1 try: file_pointer = open("C: Python 34Fake. File.

Exception Handling # PROGRAM Exception. Handing 1 try: file_pointer = open("C: Python 34Fake. File. txt", "r") print(file_pointer. read()) file_pointer. close() except: print("Something went wrong") # END.

Exception Handling # PROGRAM Exception. Handing 1 IF (There is no problem opening the

Exception Handling # PROGRAM Exception. Handing 1 IF (There is no problem opening the file) THEN try: file_pointer = open("C: Python 34Fake. File. txt", "r") print(file_pointer. read()) file_pointer. close() except: ELSE print("Something went wrong") # ENDIF; END.

Exception Handling • If we are asking for the user to input the filename

Exception Handling • If we are asking for the user to input the filename we want to open, it is very important that we include an exception handling block to make sure we deal with the case of the user typing in the wrong filename.

Exception Handling # PROGRAM Exception. Handling 2 Name. Of. File = str(input("What File would

Exception Handling # PROGRAM Exception. Handling 2 Name. Of. File = str(input("What File would you like to read: Path. Name = "C: \Python 34\" Extension = ". txt" Full. File. Name = Path. Name + Name. Of. File + Extension file_pointer = open(Full. File. Name, "r") print(file_pointer. read()) file_pointer. close() # END. ")) Without the exception block

Exception Handling # PROGRAM Exception. Handling 2 Name. Of. File = str(input("What File would

Exception Handling # PROGRAM Exception. Handling 2 Name. Of. File = str(input("What File would you like to read: ")) Path. Name = "C: \Python 34\" Extension = ". txt" Full. File. Name = Path. Name + Name. Of. File + Extension try: file_pointer = open(Full. File. Name, "r") print(file_pointer. read()) file_pointer. close() With the except: exception print("No file of that name found") block # END.

Exception Handling • Python can detect different types of exceptions, including: – Input/Output exceptions

Exception Handling • Python can detect different types of exceptions, including: – Input/Output exceptions – File indexing exceptions – Directory key exceptions – Variable naming exceptions – Syntax exceptions – Type exceptions – Argument exceptions – Divide-by-zero exceptions

Exception Handling Exception Type Description IOError Raised when trying to read or write a

Exception Handling Exception Type Description IOError Raised when trying to read or write a non-existent file Index. Error Raised when an array element that doesn’t exist is named Key. Error Raised when a dictionary key is not found Name. Error Raised when the name of variable or function is not found Syntax. Error Raised when a syntax error in the code is detected Type. Error Raised when an inappropriate type is detected Value. Error Raised when a problem with the value passed in is detected Zero. Division. Error Raised when denominator of a division is zero

Exception Handling # PROGRAM Exception. Handling 3 try: Input. Value = int(input("Please Input a

Exception Handling # PROGRAM Exception. Handling 3 try: Input. Value = int(input("Please Input a Value: print("The value input was", Input. Value) except Value. Error: print("Dude, you didn't type in a number!") # END. "))

Exception Handling # PROGRAM Exception. Handling 3 try: Input. Value = int(input("Please Input a

Exception Handling # PROGRAM Exception. Handling 3 try: Input. Value = int(input("Please Input a Value: print("The value input was", Input. Value) except Value. Error: print("Dude, you didn't type in a number!") # END. Checking for a Value. Error. "))

Exception Handling • We can handle multiple exceptions together by listing them in a

Exception Handling • We can handle multiple exceptions together by listing them in a single except clause. • For example: except(Type. Error, Value. Error):

Exception Handling # PROGRAM Exception. Handling 4 for Input. Value in (None, "Hi!"): #

Exception Handling # PROGRAM Exception. Handling 4 for Input. Value in (None, "Hi!"): # DO try: print(float(Input. Value)) except(Type. Error, Value. Error): print("Something went wrong!") # ENDFOR; # END.

Exception Handling # PROGRAM Exception. Handling 4 for Input. Value in (None, "Hi!"): #

Exception Handling # PROGRAM Exception. Handling 4 for Input. Value in (None, "Hi!"): # DO try: print(float(Input. Value)) except(Type. Error, Value. Error): print("Something went wrong!") # ENDFOR; # END. Checking for a Type. Error and Value. Error. Type. Error: float(None) Value. Error: float(“Hi!”)

Exception Handling • We can also handle multiple exceptions individually by listing them in

Exception Handling • We can also handle multiple exceptions individually by listing them in a seperate except clauses. • For example: except Type. Error: except Value. Error:

Exception Handling # PROGRAM Exception. Handling 5 for Input. Value in (None, "Hi!"): #

Exception Handling # PROGRAM Exception. Handling 5 for Input. Value in (None, "Hi!"): # DO try: print(float(Input. Value)) except Type. Error: print("Type Error: Dude, you typed in a NULL value”) except Value. Error: print("Value Error: Dude, you typed in characters") # ENDFOR; # END.

Exception Handling # PROGRAM Exception. Handling 5 for Input. Value in (None, "Hi!"): #

Exception Handling # PROGRAM Exception. Handling 5 for Input. Value in (None, "Hi!"): # DO try: print(float(Input. Value)) except Type. Error: print("Type Error: Dude, you typed in a NULL value”) except Value. Error: print("Value Error: Dude, you typed in characters") # ENDFOR; Checking for a Type. Error and Value. Error. # END. Type. Error: float(None) Value. Error: float(“Hi!”)

Exception Handling • When an exception occurs, that exception passes a system message back

Exception Handling • When an exception occurs, that exception passes a system message back to the program as well that can be printed out. • For example: except Type. Error as Sys. Message: print("System Message: ", Sys. Message)

Exception Handling # PROGRAM Exception. Handling 6 for Input. Value in (None, "Hi!"): #

Exception Handling # PROGRAM Exception. Handling 6 for Input. Value in (None, "Hi!"): # DO try: print(float(Input. Value)) except(Type. Error, Value. Error) as Sys. Message: print("Something went wrong!") print("System Message: ", Sys. Message) # ENDFOR; # END.

Exception Handling # PROGRAM Exception. Handling 6 for Input. Value in (None, "Hi!"): #

Exception Handling # PROGRAM Exception. Handling 6 for Input. Value in (None, "Hi!"): # DO try: print(float(Input. Value)) except(Type. Error, Value. Error) as Sys. Message: print("Something went wrong!") print("System Message: ", Sys. Message) # ENDFOR; # END. System Message: float() argument must be a string or a number, not 'None. Type' System Message: could not convert string to float: 'Hi!'

Exception Handling • For can also add a single else statement to the except

Exception Handling • For can also add a single else statement to the except block. • This else statement is executed if no exceptions are raised in the try block.

Exception Handling # PROGRAM Exception. Handling 7 try: Input. Value = int(input("Please Input a

Exception Handling # PROGRAM Exception. Handling 7 try: Input. Value = int(input("Please Input a Value: except Value. Error: print("Dude, you didn't type in a number!") else: print("The value input was", Input. Value) # END. "))

Exception Handling # PROGRAM Exception. Handling 7 try: Input. Value = int(input("Please Input a

Exception Handling # PROGRAM Exception. Handling 7 try: Input. Value = int(input("Please Input a Value: except Value. Error: print("Dude, you didn't type in a number!") else: print("The value input was", Input. Value) # END. ")) An ELSE statement after the EXCEPT block allows the program to let the user know that the TRY statement suceeded.

etc.

etc.