How do I test my parameter if it will raise an exception without actually raising it, using try and except?
class MyClass:
def function(parameter):
pass
parameter is an ambiguous function that may raise 1 or more of any exception, for example:
parameter = pow("5", 5)
A TypeError is raised as soon as the function is called and before the function can execute its statements.
In a comment to another answer you said: “
parameteris another function; take for example:parameter = pow("5", 5)which raises aTypeError, but it could be any type of function and any type of exception.”If you want to catch the exeption inside your function you have to call the paramenter (which I’m assuming is callable) inside that function:
Example:
This is if you really need to call your “paramenter” inside the function. Otherwise your should manage its behaviour outside, maybe with something like:
In this example, to raise an exception is
pownotfunction, so it’s a good practice to separate the the two different call, and wrap with atry-exceptstatement the code that might fail.