May 2017
Intermediate to advanced
280 pages
6h 2m
English
In this section, we will use classes, as you have not read the Chapter 11, Class and Objects, you can skip the topic and get back once you complete the class and objects.
Python allows you to define your own exceptions. Exceptions should be inherited from the Exception class.
Let's discuss with an example:
class MyException(Exception): def __init__(self, value): self.value = value def __str__(self): return (self.value) try: num = raw_input("Enter the number : ") if num == '2': raise MyException("ohh") else : print "number is not 2"except MyException : print "My exception occurred"
Do not be afraid to see the full code.
Let's see the class part:
class MyException(Exception): def __init__(self, value): self.value ...
Read now
Unlock full access