Exceptions

Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Alex Yarosh

Content Quality Analyst @ DataCamp

a = 1
a / 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    1/0
ZeroDivisionError: division by zero
a = [1,2,3]
a[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    a[5]
IndexError: list index out of range
a = 1
a + "Hello"
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
    a + "Hello"
TypeError: unsupported operand type(s) for +: /
'int' and 'str'
a = 1
a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    a + b
NameError: name 'b' is not defined
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Exception handling

  • जब exception उठे, तो प्रोग्राम को रुकने से बचाएँ
  • try - except - finally:
try:
  # कुछ कोड चलाने की कोशिश करें

except ExceptionNameHere: # यदि ExceptionNameHere हो, तो यह कोड चलाएँ
except AnotherExceptionHere: #<-- कई except ब्लॉक # यदि AnotherExceptionHere हो, तो यह कोड चलाएँ ...
finally: #<-- वैकल्पिक # किसी भी हालत में यह कोड चलाएँ
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Exceptions उठाना

  • raise ExceptionNameHere('Error message here')
def make_list_of_ones(length):
    if length <= 0:
       raise ValueError("Invalid length!")  # <--- प्रोग्राम रोकेगा और error उठाएगा
    return [1]*length   
make_list_of_ones(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    make_list_of_ones(-1)
  File "<stdin>", line 3, in make_list_of_ones
    raise ValueError("Invalid length!")
ValueError: Invalid length!
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Exceptions क्लासेस हैं

  • standard exceptions, BaseException या Exception से विरासत में मिलती हैं
    BaseException
    +-- Exception
        +-- ArithmeticError                     # <--- 
        |    +-- FloatingPointError
        |    +-- OverflowError
        |    +-- ZeroDivisionError              # <---
        +-- TypeError
        +-- ValueError
        |    +-- UnicodeError
        |         +-- UnicodeDecodeError
        |         +-- UnicodeEncodeError
        |         +-- UnicodeTranslateError
        +-- RuntimeError
       ...
    +-- SystemExit
    ...
    
1 https://docs.python.org/3/library/exceptions.html
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

कस्टम exceptions

  • Exception या इसकी किसी subclass से inherit करें
  • आम तौर पर खाली क्लास होती है
class BalanceError(Exception): pass
class Customer:
   def __init__(self, name, balance):
    if balance < 0 :
       raise BalanceError("Balance has to be non-negative!")
    else:
       self.name, self.balance = name, balance

Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग
cust = Customer("Larry Torres", -100)
Traceback (most recent call last):
  File "script.py", line 11, in <module>
    cust = Customer("Larry Torres", -100)
  File "script.py", line 6, in __init__
    raise BalanceError("Balance has to be non-negative!")
BalanceError: Balance has to be non-negative!
  • Exception ने constructor को बीच में रोका → object नहीं बना
cust
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    cust
NameError: name 'cust' is not defined
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

कस्टम exceptions पकड़ना

try:
  cust = Customer("Larry Torres", -100)
except BalanceError:
  cust = Customer("Larry Torres", 0)
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

अभ्यास करते हैं!

Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Preparing Video For Download...