Custom Exception in Python


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





Comments

Popular posts from this blog

How To Create .ENV File in Python

Create a Large file for Test Data Processing using Python

How to solve the Liner Equation using Python