FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/exceptions/try_except_else.py at main · thomasWeise/programmingWithPythonCode · GitHub
thomasWeise
programmingWithPythonCode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programmingWithPythonCode
/
exceptions
/
try_except_else.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
22 lines (17 loc) · 1.07 KB
Breadcrumbs
programmingWithPythonCode
/
exceptions
/
try_except_else.py
Copy path
File metadata and controls
22 lines (17 loc) · 1.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""Demonstrate `try...except..else`."""
from
sqrt_raise
import
sqrt
# Import our sqrt function.
sqrt_of_1_div_0
:
float
# Declare this variable, but do not assign it.
try
:
# If this block raises DivisionByZero, we continue at `except`.
sqrt_of_1_div_0
=
sqrt
(
1
/
0
)
# This is a DivisionByZero!
except
ZeroDivisionError
as
de
:
# Catch and print a ZeroDivisionError.
print
(
f"We got a division-by-zero error:
{
de
}
."
,
flush
=
True
)
else
:
# This code is not executed because an exception occurred.
print
(
f"
\u221A
(1/0)
\u2248
{
sqrt_of_1_div_0
}
"
)
# Never reached.
sqrt_3
:
float
# Declare this variable, but do not assign it.
try
:
# If this block raises ArithmeticError, we continue at `except`.
sqrt_3
=
sqrt
(
3.0
)
# This will work just fine and raise no error.
except
ZeroDivisionError
:
# We catch and print the ZeroDivisionError.
print
(
f"We got a another division-by-zero error!"
,
flush
=
True
)
else
:
# This code is executed because no exception occurs.
print
(
f"
\u221A
3
\u2248
{
sqrt_3
}
"
)
# Always executed.
print
(
"The program is now finished."
)
# We do get here.
Back
|
FazBrowse Home
|
New Git URL