Python Forum

Full Version: Howto try catch
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

I want to exceute some commands in a try environment. How can I do this in python?

Thanks
Consuli
try:
   # what you want to try
except except_condition:
   # Except code here
The most important part is to give user a meaningful message Snooty
>>> 1 / 0
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 
>>> try:
...     1 / 0
... except ZeroDivisionError:  
...     print('Are you a stupid donkey,you can not divide by 0')
...     
Are you a stupid donkey,you can not divide by 0