Updated code examples to Python 3 (#20765)

This commit is contained in:
Ken Nguyen
2018-11-03 13:20:23 -04:00
committed by Christopher McCormack
parent d81d8c77b5
commit 575cf064af

View File

@ -10,24 +10,26 @@ To solve this problem in python we can use `try` and `except`
Example:
```shell
>>> try:
>>> . . . print "this is not a string "+1
>>> except:
>>> . . . print "error"
error
```
and if you want to get error messages in more detail from your code, you can add arguments `except Exception as err`
```python
try:
print("this is not a string " + 1)
except:
print("error")
```shell
>>> try:
>>> . . . print "this is not a string "+1
>>> except Exception as err:
>>> . . . print "error:\n"+str(err)
error:
cannot concatenate 'str' and 'int' objects
>>> error
```
And if you want to get error messages in more detail from your code, you can add arguments `except Exception as err`
```python
try:
print("this is not a string " + 1)
except Exception as err:
print("error:\n" + str(err))
>>> error:
>>> must be str, not int
```
More Information:
Errors and Exceptions <a href='https://docs.python.org/2/tutorial/errors.html' target='_blank' rel='nofollow'>documentation</a>.
Errors and Exceptions <a href='https://docs.python.org/3.6/tutorial/errors.html' target='_blank' rel='nofollow'>documentation</a>.