Python mai coding krte samay kabhi na kabhi apko error to jrur mili hogi jaise
print('hello)
Output:
Syntax error
2+'s'
Output:
Type error
So is tarah ki error jo python nhi samajh pa raha hai ise hm exception kahte hai. Hm is tarah ke exception ko handle kr skte hai, dekho hota kya hai jb exception aati hai to python interpreter program ko stop kr deta hai aur error throw kr deta hai, aur hame output mai error mil jati hai ,to hm error ko handle kr skte hai try aur except keyword ka use kr ke so , try aur except error ko handle krte hai aur baki ke code ko continue rakhne mai madad krte hai.
So , jb bhi apko lage ki yaha error hone ke chances hai ise try keyword ke andar rakhe, aur us exception ki handling hm except ke andar rakhenge
Aayiye ise example ki madad se aur clear krte hai...
----------------------------------------------------------------------------------
try:
2+'s'
except:
print('There was a type error')
print('Rishabh Gupta')
Output:
There was a type error Rishabh Gupta
#2+'s' ye ek type error agar error hai to except block of code execute ho jayega
#Aur hm dekh skte hai ki baki rest of code bhi aaram se execute ho gya , error aane ke baad bhi to ye bahut bada advantage hai try aur except keyword ka
----------------------------------------------------------
try:
2+2
except:
print('Something missing in the cury')
Output:
no output
# try block mai jo code hai usme koi error nhi hai, to except block of code kabhi execute nhi hoga
#except block of code tabhi execute hoga jb ap try mai kuch meaning less karoge
-------------------------------------------------------
Finally keyword
Finally ke andar jo block of code hai wo hamesha execute hoga agar koi exception hai to bhi aur agar koi exception nhi hai to bhi
Examples
------------------------------------------------------------
try:
2+2
except:
print('Something missing in the cury')
finally:
print('Finally this is printed')
Output:
Finally this is printed# try block of code mai koi bhi error nhi hai , to except block of code execute nhi hoga finally execute hoga-------------------------------------------------------------try: 2+'s' except: print('Not tasty') finally: print('This is cleaning stage')
Output:
Not tasty This is cleaning stage# finally hamesha execute hoga agar error hai to bhi aur agar error nhi hai to bhi-----------------------------------------------------------General exception and specific exception#General exceptiontry: 1/0 except: print('Something else is needed')
Output:
Something else is needed#Agar ap simply except likh do ge to ise general exception kaha jayega#Specific exceptiontry: 1/0 except ZeroDivisionError: print('Chilly not enough')
Output:
Chilly not enough#Agar ap specificialy mention kr rahe ho ki ye kis type ki error hai to ise hm specific exception kahenge#Agar ap specificaly error mention nhi bhi krte ho to hme output wahi milega# lekin agar ap galat error mention krte ho to hme program mai error milegi------------------------------------------------------------try: 2+'s' except TypeError: print('Salt is not enough') finally: print('This is cleaning stage')
Output:
Salt is not enough This is cleaning stage------------------------------------------------------------------------------------
Comments
Post a Comment