Exceptions raised by Python

Let's add a new line to the main() function which takes the square-root of -1:

def main():
print(sqrt(9))
print(sqrt(2))
print(sqrt(-1))

If we run that, we get a new exception:

$ python3 sqrt.py
3.0
1.41421356237
Traceback (most recent call last):
File "sqrt.py", line 14, in <module>
print(sqrt(-1))
File "sqrt.py", line 7, in sqrt
guess = (guess + x / guess) / 2.0
ZeroDivisionError: float division

What has happened is that Python has intercepted a division by zero, which occurs on the second iteration of the loop, and raised an exception – a ZeroDivisionError.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset