File tree 5 files changed +48
-0
lines changed 5 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
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/ ) .
Original file line number Diff line number Diff line change
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." )
Original file line number Diff line number Diff line change
1
+ number = 10
2
+ assert number < 5 , f"The number should not exceed 5. ({ number = } )"
3
+ print (number )
Original file line number Diff line number Diff line change
1
+ try :
2
+ with open ("file.log" ) as file :
3
+ read_data = file .read ()
4
+ except FileNotFoundError as fnf_error :
5
+ print (fnf_error )
Original file line number Diff line number Diff line change
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." )
You can’t perform that action at this time.
0 commit comments