Untitled Document
Exception Handling - try , except , else , finally
1. Introduction Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Introduction 
       
       # Exception handling is used to handle known errors or issues going to happen in future 
       # if such errors occur then our program should handle it successfully without having abnormal termination 
       # and provide valid messages for it . 
       # We have different types of inbuilt exception written in Python 
       # like - File not found , zero dvision error etc . 
       # we will create some customize / user defined exception as well     

       
2. Program - Simple Example of try block Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Simple Example of try block 
            
       # # EX - 1 
       # Here is the simple Example where we will try to open a non existitent file 
       try:
       	f = open('textfile.txt')
       except Exception:
       	print("Sorry . This file doesn't exist")
       
       # Now in above code ; the line written in try block are executed and if 
       # there is any error while opening file then the exception message will be printed 
       # Now lets assume that we give correct file and add one more line 
       # where we are doing bad assignment of a variable ; then in that case also we 
       # will get same exception message which is not valid 
      
       # EX - 2 One More Example of Similar Pattern - this is to match mangadaku.com name from URL string 
       
       try:
       	f = open('test.txt')
       	var = bad_var
       except:
       	print("Sorry . This file doesn't exist")
    
3. Program - Adding Multiple Exception Block Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Adding Multiple Exception Block 
       
       # In Ex -1 we have seen that even for bad varibale assignment we got file not found custom message 
       # Two get the correct exception ; lets add one more Genereic exception to Ex - 1 code 
       
       try:
       	f = open('test.txt')
       	var = bad_var
       except FileNotFoundError:
       	print("Sorry . This file doesn't exist")
       except Exception:
       	print("Something went wrong !! ")
           
4. How to get the Correct Exception messages for valid errors Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # How to get the Correct Exception messages for valid errors 
       
       # We can get the list of Excetion names from pYthin Documentation 
       # but if we don't know the Exceptin names and still want to print
       # Python valid exception messages ; then refer below code  
       
       # EX - A)
       
       try:
       	f = open('test.txt')
       	var = bad_var
       except FileNotFoundError as e:
       	print(e)
       except Exception as e:
       	print(e)
       
       # EX - B)
       
       try:
       	f = open('filetest.txt')
       	var = bad_var
       except FileNotFoundError as e:
       	print(e)
       except Exception as e:
       	print(e)
       
       # IMP - ALWAYS PUT STANDARD EXCEPTION like - FileNotFoundError first in sequence 
       # If you put generic Exception on top before STANDARD EXCEPTION then python will execute generic exception 
       # and you may end up with issues

       
5. Else Block in Exception Handling Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Else Block in Exception Handling
       
       # The Else Block will be executed only when the try block is executed successfully . i.e without any error or exception 
       # Here is the simple Example
       # In below example try block executed successfully hence in the lese block file was read successfully
       
       try:
       	f = open('test.txt')
       except FileNotFoundError as e:
       	print(e)
       except Exception as e:
       	print(e)
       else:
       	print(f.read())
       	f.close()
       
       
6. Finally Block - will be executed No Matter whether you get Exception in try block or not Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Finally Block - will be executed No Matter whether you get Exception in try block or not
       # here are two quick and simple examples of finally Block 
       
       # EX - A)
       try:
       	f = open('test.txt')
       except FileNotFoundError as e:
       	print(e)
       except Exception as e:
       	print(e)
       else:
       	print(f.read())
       	f.close()
       finally:
       	print("Execting Finally ....")
       
       # EX - B Code with Non existent file so we sould get valid exception
       
       try:
       	f = open('test12.txt')
       except FileNotFoundError as e:
       	print(e)
       except Exception as e:
       	print(e)
       else:
       	print(f.read())
       	f.close()
       finally:
       	print("Execting Finally ....")
 
       
7. You can raise exception by your own ; using raise keyword Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # You can raise exception by your own ; using raise keyword
       # here is the simple example  
       
       try:
       	f = open('test.txt')
       	if f.name == 'test.txt':
       		raise Exception
       except FileNotFoundError as e:
       	print(e)
       except Exception as e:
       	print("Error !!")
       else:
       	print(f.read())
       	f.close()
       finally:
       	print("Execting Finally ....")
 
       
Untitled Document