Now that we have substantial knowledge of Python, let’s look at one very important thing with software: error handling. Like most other languages, Python also provides error handling with exceptions. Let’s have a look in how Python exception handling works.
Python exception handling
Basically, each progress of error-handling starts with a try-statement. This is a block where an error might occur. If an error occurs in the try-block, an exception is raised in Python. The interpreter then looks if there is a surrounding exception handler. It is best to handle exceptions as detailed as possible, since it will prevent errors later in the program. Python has a huge list of pre-defined exceptions, so it is easy to handle them without the need for own exceptions. A try-Block might also have a finally-block. This is useful if you worked with files or opened some connections. In the finally-block, you can close the connections. Note that the finally-block is executed every time, independent of an error or not. The syntax for Exceptions is this:
If you look for exceptions in the Python documentation, you have to look them up with the “Error” appending. Python doesn’t call them “Exceptions” – even though the base-class is called like that. In the following sample, we will create a division by zero error. Therefore, we define a method “divide” which takes two parameters. We surround the division with the try-statement and check for the ZeroDivisionError. Note that the finally block is executed in all calls to the method.
def divide(val1, val2): try: return val1 / val2 except ZeroDivisionError: print("Division by zero - return 0 instead") return 0 finally: print("Cleanup everything") res = divide(2, 3) res2 = divide(2, 0) print("The result for res is: " + str(res) + " and for res2 it is: " + str(res2))
Cleanup everything Division by zero - return 0 instead Cleanup everything The result for res is: 0.6666666666666666 and for res2 it is: 0
So, this was easy, wasn’t it? Now, let’s have a look at how to raise your own exceptions.
Raise exception in Python
Basically, an exception can be “thrown” with the “raise” statement. Python is much more modest with that. C-like languages throw exceptions at you, whereas Python just kindly raises one ;). The statement to raise an exception is written like this:
In our next sample, we want to raise an error for a car that drives too fast. Therefore, we first need to create our own exception. All exceptions inherit from “Exception”. So, we first create a class that inherits from that. We call the error “TooFastError”. We add no further functionality and just write pass. This instructs Python to continue with other logic. We then define a function “accelerate”, which gets exactly one parameter – speed. If speed is higher than 100, we now raise our TooFastError. Let’s try it:
class TooFastError(Exception): pass def accelerate(speed): if speed > 100: raise TooFastError("You can't drive at " + str(speed) + " the overall speed limit is 100!") else: print("Ok, let's go!") accelerate(20) accelerate(110)
Ok, let's go! TooFastErrorTraceback (most recent call last) <ipython-input-8-9be2666bf1f4> in <module> 9 10 accelerate(20) ---> 11 accelerate(110) <ipython-input-8-9be2666bf1f4> in accelerate(speed) 4 def accelerate(speed): 5 if speed > 100: ----> 6 raise TooFastError("You can't drive at " + str(speed) + " the overall speed limit is 100!") 7 else: 8 print("Ok, let's go!") TooFastError: You can't drive at 110 the overall speed limit is 100!
Isn’t it beautiful to raise your own exceptions :)? In our next tutorial we will have a look at decorators in Python and at the Dataclass.
If you are not yet familiar with Spark, have a look at the Spark Tutorial i created here. Also, I will create more tutorials on Python and Machine Learning in the future, so make sure to check back often to the Big Data & Data Science tutorial overview. I hope you liked this tutorial. If you have any suggestions and what to improve, please feel free to get in touch with me! If you want to learn more about Python, I also recommend you the official page.