.feed-links {display:none !important;} -->

Python exceptions

 Sometimes it is not possible to perform certain operations in mathematics, such as division by zero. For example, Python thinks that this knowledge is obvious and if you ask it to divide a number by zero, it won't bother to swarm you.

We are teasing, we will try:

>>>  5 / 0 
Traceback  ( MOST  recent  call  last ): 
  File  "<stdin>" ,  line  1 ,  in  < Module > 
ZeroDivisionError :  integer  division  or  modulo  by  zero

We notice first of all that python is certainly severe but fair; he tells us why he is not happy: ZeroDivisionError .

This example is irrelevant but it is quite possible to use a variable as the denominator and at this point how to avoid this error?

One solution would be to check the value of the variable and if it is equal to 0, we cancel everything.

Another solution would be to anticipate that there might be an error and in the event of an error provide specific instructions.

Try except

Try means "to try" in English, this key word makes it possible to try an action and if the action fails we can give it other instructions in a block except .
>>>  v  =  0 
>>>  w  =  5 
>>>  try : 
...      w / v 
...      print ( "Ok no error" ) 
...  except : 
...      print ( "Error" ) 
.. .  
Error

So why use try , in the end we don't care if there is an error?

Well not totally, this kind of error is blocking, i.e. if the instructions are executed in a script, the script stops and it becomes a bug

Target errors

The syntax exposed above responds to any type of error, but it is possible to refine the error handling.

For example what happens if we divide a number by letters?

>>>  5 / "olive" 
Traceback  ( MOST  recent  call  last ): 
  File  "<stdin>" ,  line  1 ,  in  < Module > 
TypeError :  unsupported  operand  types ( s )  for  / :  'int'  and  'str'

We notice that python displays an error but it is different from the one caused by the division by 0. Here the error is TypeError .

It is possible in python to execute instructions depending on the type of error. For example if the value is 0 we would like to display a message and if the denominator is text we would like to be able to display another message to the user.

>>>  v  =  0 
>>>  w  =  5 
>>>  try : 
...      w / v 
...      print ( "Ok no error" ) 
...  except  TypeError : 
...      print ( "Please use numbers " ) 
...  except  ZeroDivisionError : 
...      print ( " Please do not use the 0 " ) 
...  
Thank you  to  do  not  use  the  0

and in the case where the variable is equal to " olive tree ":

>>>  v  =  "olivier" 
>>>  w  =  5 
>>>  try : 
...      w / v 
...      print ( "Ok no error" ) 
...  except  TypeError : 
...      print ( "Thank you use the numbers " ) 
...  except  ZeroDivisionError : 
...      print ( " Please do not use the 0 " ) 
...  
Thank you  for using numbers

Finally

The keyword is used finally to execute instructions regardless of the errors generated or not (and even if there is one return ). In all cases the instructions placed in finally will be executed.

try : 
    pass  
except : 
    pass 
finally : 
    print ( "Execution" )

No comments:

Post a Comment