Python Exception Handling

📔 : Python 🔗

Exception Handling , program में आयी unwanted errors को handle करने का एक process / method है। इन unwanted errors की वजह से कभी कभी program terminate हो जाता है। हालाँकि program का terminate होना Exception पर depend करता है कि Exception किस तरह की है।

Understanding Exception : Exceptions कुछ unwanted events होती हैं , जो program execution के समय आती हैं और program को disrupts करती हैं।

Types Of Errors

  1. Syntax Errors
  2. Run Time Errors
  3. Logical Errors

Python Syntax Errors

Python में Syntax Errors program में syntax सही न होने के कारण आती हैं , इन्हे parse errors भी कहते हैं जो कि interpret time पर आती हैं। Syntax error जब रहेगी तब तक program बिलकुल भी run नहीं होगा।
For Example -

#here we are missing right paranthysis. So Oviously we will get an error.
print(name
  File "<exception_ex.py>", line 2
    print(name
             ^
SyntaxError: unexpected EOF while parsing

ऊपर example में print() function का parenthesis ) close न करने की वजह से error आयी।

Python Run Time Errors

Runtime Errors, program के running time पर किसी गलत logic या गलत input की वजह से आती हैं, run time errors को ही exceptions कहते हैं। Run time error आने तक पूरा program proper run होता है और output भी proper generate होता है।
For Example -

#here we are accessing an undefined variable.
print(name)
Traceback (most recent call last):
  File "<exception_ex.py>", line 2, in <module>
    print(name)
NameError: name 'name' is not defined

example में सब कुछ ठीक है शिवाय , undefined variable name के , जब तक वो line execute नहीं होगी तब तक सब ठीक रहेगा। जैसे ही वह line run होगी बैसे ही error generate हो जायगी।


जैसे अगर आप इस line से पहले जो भी valid code लिखेंगे , वो proper run होकर output generate करेगा।

Copy Fullscreen Close Fullscreen Run
print("it will run.")
#now print undefined variable.
print(name)
Output
C:\Users\Rahulkumar\Desktop\python>python exception_ex.py
it will run.
Traceback (most recent call last):
  File "<exception_ex.py>", line 3
    print(name)
NameError: name 'name' is not defined

Python Logical Errors

Logical Errors , program में apply किये गए logic में mistake की वजह से आती हैं। इन Logical Errors की वजह से हमें desirable output नहीं मिलता। Logical Errors को बिना किसी tool के handle करना मुश्किल है।
For Example एक ऐसा loop जो कभी ख़त्म ही न हो -

#a endless loop can terminate / destroy your program.
while True :
    print('Hello')
print('It will never gonna print')

How to handle ?

Syntax Error और Logical Errors को तो handle नहीं किया जा सकता है , code करते समय ही आपको इस बात का ध्यान रखना पड़ेगा। Run Time Errors को Handle करने के लिए Python में कुछ statements हैं जिनकी help से इन errors को handle कर सकते हैं।

  1. try except.
  2. try except finally.

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers