Posts

Showing posts with the label try

Custom Exception in Python

Image
Concept When an error occurs, or exception as we call it, Python will normally stop and generate an error message But this is not expected in Real Life Application, So these exceptions can be handled using the  try, except  statement. Code Example 1: x = 10 if x == 10:   raise ValueError elif x == 20:   raise KeyboardInterrupt elif x == 30:   raise Exception("My Choice") else:   print("All Okay")   Output:  Traceback (most recent call last): File "./prog.py", line 4, in ValueError Quiz Level 1 Quiz Level 2 Request Me

Exception Handling In Python

Image
   Concept When an error occurs, or exception as we call it, Python will normally stop and generate an error message But this is not expected in Real Life Application, So these exceptions can be handled using the  try, except  statement. Code Example As we all know,  0/n = 0 and n/0 = ∞ Example 1: try:     result = 0/10     print("All Good") except Exception as e:     result  = 0     print("An exception occurred : ", e) else:     print("No Exception Occurred") finally:     print("Result is : ", result)   Output : All Good No Exception Occurred Result is : 0   Example 2: try:     result = 0/10     print("All Good") except Exception as e:     result  = 0     print("An exception occurred : ", e) else:     print("No Exception Occurred") finally:     print("Result is : ", result)   Output:  An e...