PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# dev@brattham.se (2022-06-24) v1.0
"""Demo of handling of Error and exceptions.
-> FIRST TIME input an INTEGER, to se how try / except / else / finally \
handel the the 'no error case'.
-> NEXT TIME input a NON NUMBER CHARACTER/S that int() can't handle, like X, to se how try / except / else /finally \
handle the 'error case'.
"""
print("""In The Python Tutorial (8.2 Exceptions), at https://docs.python.org/3/tutorial/, you can read: \
”Errors detected during execution are called exceptions”.
It is possible to catch exceptions with a try/except statment.
This part, and the text above, is printed before the try/except \
statment, and will therefore always be printed.""")
try:
n = int(input() or 'x')
print(
f"\n\t=> {n = } - This part is printed from inside the"
" try statement, and will be printed just if input is (converted "
"to) an intger."
)
except ValueError:
print(
"\n\t=> Error detected! - This part is printed from inside the "
"except statement, and will be printed just if the input "
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run