Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
Exception | A base class for most error types |
---|---|
AttributeError | Raised by syntax obj.foo, if obj has no member named foo |
EOFError | Raised if “end of file” reached for console or file input |
IOError | Raised upon failure of I/O operation (e.g., opening file) |
IndexError | Raised if index to sequence is out of bounds |
KeyError | Raised if nonexistent key requested for set or dictionary |
KeyboardInterrupt | Raised if user types ctrl-C while program is executing |
NameError | Raised if nonexistent identifier used |
StopIteration | Raised by next(iterator) if no element; see Section 1.8 |
TypeError | Raised when wrong type of parameter is sent to a function |
ValueError | Raised when parameter has invalid value (e.g., sqrt(−5)) |
Example 1
def div():
try:
numer, denom = map(int, input("Enter a numerator, denominator: ").strip().split())
print(f"The fraction ratio is {numer / denom}")
except Exception as error:
print("The Error is", error)
else:
print("The operation done Successfully.")
finally:
print("The Mission Done.")
div()
Enter a numerator, denominator: 8 2
The fraction ratio is 4.0
The operation done Successfully.
The Mission Done.
div()
Enter a numerator, denominator: 8 0
The Error is division by zero
The Mission Done.