Updated code examples to Python 3 (#20765)
This commit is contained in:
committed by
Christopher McCormack
parent
d81d8c77b5
commit
575cf064af
@ -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>.
|
||||
|
Reference in New Issue
Block a user