While the try/except allows you to handle an exception and do something other than crash the program, it is also the case that you may actually WANT to raise an exception if the preconditions for a function are violated. The assert statement asserts that conditions you want to be true, are true.
You have seen the white-box testing crop up in an earlier page here. You have also seen examples of test cases, where we know what we should have as an output for a specific set of inputs. assert and raise are frequently used instead of a try/except, where you want the code to test if something is true, and if it is not- raise an error, thus halting the program.
These are often used in what are called unit tests.
The assert works like a one-line conditional check- if the thing we assert to be True is not, it will raise an AssertionError and post the message you specify after the assertion.
You can see that we could send invalid arguments to this function. The "type-hinting" in the first line (hours:float and rate:float) do not ensure correct data types. The result could then be invalid or cause an exception when we try to do the calculation.
Invalid arguments in this case could be:
Negative numbers- the result would be that the employee pays the employer for the privilege of working
Strings or other non-numerical data types
While the negative numbers will not cause an error, we know there is something wrong.
Strings will raise an error at the comparison line if hours <= 39:
TypeError: '<=' not supported between instances of 'str' and 'int'
What we could do is to check- before we do anything else- that the data types are correct. We can do this with the assert statement, and if the "thing" is not the correct type then raise an Exception. If the thing we assert to be True is not, it will raise an AssertionError and post the message you specify after the assertion.
Here the code has been implemented for the wages function. Note that if wages or rate are non-numerical types, our code will raise an exception in the form of an AssertionError followed by the message.
Another thing we could do is:
if type(hours) is not float or type(hours) is not int:
raise TypeError('Hours must be a valid number type')
but of course add the details omitted in this example.
The most common exception classes we will likely use or encounter include:
ImportError: an import fails;
IndexError: a list is indexed with an out-of-range number;
NameError: an unknown variable is used;
SyntaxError: the code can't be parsed properly;
TypeError: a function is called on a value of an inappropriate type;
ValueError: a function is called on a value of the correct type, but with an inappropriate value.
but there are a lot of possible ones that arise, depending on what kind of tasks you are performing. Python has several other built-in exceptions, such as ZeroDivisionError and OSError. Third-party libraries also often define their own exceptions.
These last definitions are taken from the mini-learning site SoloLearn.
Some common Exceptions in Python