Creating an error is referred to as raising an exception. You saw some examples of exceptions in the previous section. You can also define your own exceptions of a predefined type or use an exception of an unspecified type. Raising an exception is done with a command like this:
raise Exception("Something went wrong")
Here an exception of an unspecified type was raised.
It might be tempting to print out error messages when something goes wrong, for example, like this:
print("The algorithm did not converge.")
This is not recommended for a number of reasons. Firstly, printouts are easy to miss, especially if the message is buried in many other messages being printed to your console. Secondly, and more importantly, it renders your code unusable by other code. The calling code will not read what you printed and will not have a way of knowing that an error occurred and therefore has no way of taking care of it.
For these reasons, it is always better...