Python Exception – Try Except in Python In Hindi

Python

Python ExceptionTry Except in Python In Hindi – Is Post Me Python Exception Handling in Hindi And Try Except And Finally in Hindi Ko Dekhne Wale Hai |

Python Exception Handling in Hindi

Python Exception Handling Ka Istemal Error Ko User Site Se Rokne Ke Liye Kiya Jata Hai, Yani Ki Simple Way Me Kaho To Iska Istemal Error Ko Rokne Ke Liye Kiya Jata Hai And Ise Ham Example Ke Sath Understand Karte Hai |

Python Exception Handling Me 3 Functions Hote Hai|

  • try
  • except
  • finally

Aap Upper Dekh Sakte Hai 3 Functions Ko , Inhe Ham Ek Ek Karke Examples Ke Sath Understand Karte Hai |

Try Except Finally in Python In Hindi

Ab Ham Try Except And Finally Function In Python Ko Ek ek Karke Understand Karte Hai, And Inke Examples Bhi Dekhte Hai |

Try Function Ka Istemal hota Hai ki Try Kro, Agar Program Execute Hota Hai To Thik Hai Nhi Hota Hai To Ise Leave Karo |

Except Function Ka Istemal Hota Hai Ki Jo Try Me Leave Kr Diya To Use Except Karo |

Iska Ab Ham Examples Ke Sath Dekhte Hai Or Program Ko Understand Karte Hai |

Example:

num1 = input("Enter First Number :")
num2 = input("Enter Second Number :")
try:
  print("The Sum Of These Two Numbers Is :", int(num1) + int(num2))
except Exception as e:
  print(e)

print("End Program") 

Aap Upper Dekh Sakte Hai Ki Hamne num1 And num2 Input Me Liya Hai, And Uske Bad try:( ke Under num1 And num2 Ko Add Kiya Hai ) Uske Bad except Exception as e:(Ke Under Jo Bhi Error Hoga Wo Uske Under Print Ho Jayega) |

Output:

Enter First Number :10
Enter Second Number :20
The Sum Of These Two Numbers Is : 30
End Program

Example 2:

num1 = input("Enter First Number :")
num2 = input("Enter Second Number :")
try:
  print("The Sum Of These Two Numbers Is :", int(num1) + int(num2))
except Exception as e:
  print(e)

print("End Program") 

Output:

Enter First Number :
Enter Second Number :20
invalid literal for int() with base 10: ''
End Program

Aap Upper Program Ko Dekh Sakte Hai, Hamne Ouput Line 1 Me Blank Input Diya And Output Line 2 Me 20 Diya , Uske Bad Program Try Me Chala Gaya Or Wala Par Add Karne Ki Koshish Ki Lekin Line 1 Me Hamne Kuch Bhi Input Nhi Kiya To Uske except Me Outline Line 3 Me Print Kr Diya |

Example 3:

Niche Diye Gaye COde Me Hamne Ek File Open Ki Hai Read Mode Me Or Ham Koshish Kar Rahe Write Karne Ki, Isliye Program Except Me Jakar Print line ko Print Kar Dega And End Me finally Me Jakar File Ko Close Kar Dega |

try:
  f = open("demofile.txt")
  f.write("Lorum Ipsum")
except:
  print("Something went wrong when writing to the file")
finally:
  f.close()

Output:

Something went wrong when writing to the file

Leave a Reply

Your email address will not be published. Required fields are marked *