FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programmingWithPythonCode/exceptions/try_except_nested_2.py at main · mlecjm/programmingWithPythonCode · GitHub
mlecjm
programmingWithPythonCode
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programmingWithPythonCode
/
exceptions
/
try_except_nested_2.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
17 lines (13 loc) · 834 Bytes
Breadcrumbs
programmingWithPythonCode
/
exceptions
/
try_except_nested_2.py
Copy path
File metadata and controls
17 lines (13 loc) · 834 Bytes
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
"""Demonstrate nested `try...except` blocks."""
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 an error, we continue at `except`.
# sqrt_of_1_div_0 only gets assigned if sqrt(1 / 0) succeeds...
sqrt_of_1_div_0
=
sqrt
(
1
/
0
)
# This produces a ZeroDivisionError.
except
ZeroDivisionError
as
de
:
# Catch an ZeroDivisionError.
print
(
f"We got a division-by-zero error:
{
de
}
."
,
flush
=
True
)
try
:
# Nesting try-except blocks is totally fine.
sqrt_of_1_div_0
=
sqrt
(
1
/
0
)
# Let's try it again!
except
ZeroDivisionError
as
de
:
# Another ZeroDivisionError?
print
(
f"Yet another division-by-zero error:
{
de
}
."
,
flush
=
True
)
print
(
"The program is now finished."
)
# This time, we do get here.
Back
|
FazBrowse Home
|
New Git URL