Skip to content

Commit d7659cb

Browse files
martin-martinKateFinegangahjelle
authored
Add materials for exceptions update (#493)
* Add materials for exceptions update * Fix linter flag * Final QA --------- Co-authored-by: KateFinegan <[email protected]> Co-authored-by: gahjelle <[email protected]>
1 parent e305a57 commit d7659cb

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

python-exceptions/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Exceptions: An Introduction
2+
3+
This folder provides the code examples for the Real Python tutorial [Python Exceptions: An Introduction](https://realpython.com/python-exceptions/).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class PlatformException(Exception):
2+
"""Incompatible platform."""
3+
4+
5+
def linux_interaction():
6+
import sys
7+
8+
if "linux" not in sys.platform:
9+
raise PlatformException("Function can only run on Linux systems.")
10+
print("Doing Linux things.")
11+
12+
13+
try:
14+
linux_interaction()
15+
except PlatformException as error:
16+
print(error)
17+
print("The linux_interaction() function wasn't executed.")

python-exceptions/low.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
number = 10
2+
assert number < 5, f"The number should not exceed 5. ({number=})"
3+
print(number)

python-exceptions/open_file.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
try:
2+
with open("file.log") as file:
3+
read_data = file.read()
4+
except FileNotFoundError as fnf_error:
5+
print(fnf_error)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def linux_interaction():
2+
import sys
3+
4+
if "linux" not in sys.platform:
5+
raise RuntimeError("Function can only run on Linux systems.")
6+
print("Doing Linux things.")
7+
8+
9+
try:
10+
linux_interaction()
11+
except RuntimeError as error:
12+
print(error)
13+
else:
14+
try:
15+
with open("file.log") as file:
16+
read_data = file.read()
17+
except FileNotFoundError as fnf_error:
18+
print(fnf_error)
19+
finally:
20+
print("Cleaning up, irrespective of any exceptions.")

0 commit comments

Comments
 (0)