Python Exception Handling
Links: 108 Python Index
Exception Handling¶
try, except and finally¶
- Exceptions are propagated through the call stack in unhandled.
- For example let us consider a program where we have a function
A
that calls functionB
, which in turn calls functionC
. - If an exception occurs in function
C
but is not handled inC
, the exception passes toB
and then toA
. - If never handled, an error message is displayed and our program comes to a sudden unexpected halt.
- For example let us consider a program where we have a function
- We handle exceptions using
try except
block.
Almost every class inherits from the Exception
class.
-
Catching multiple and specific exceptions
-
We can raise exceptions using the
raise
keyword.- Raising a general exception:
raise Exception("some message")
- Raising a specific exception:
raise ValueError("Some thing is wrong")
- Raising a general exception:
-
finally
clause is executed no matter what: -
Handling vs Raising exceptions:
Custom Exceptions¶
class GitHubApiException(Exception):
def __init__(self, status_code):
if status_code == 403:
message = "Rate limit reached. Please wait a minute and try again."
else:
message = f"HTTP Status Code was: {status_code}."
super().__init__(message)
-
Or we can have a custom message
-
Or have multiple parameters and print them as a single message:
Assertions¶
Assertions are most useful in tests.
- Using assertions
- Assertions are used to raise an exception if a condition is not met.
import sys
assert ('linux' in sys.platform), "This code runs on Linux only."
# if the program is run on a different system then we will get the following error
Traceback (most recent call last):
File "<input>", line 2, in <module>
AssertionError: This code runs on Linux only.
- Handling assertions gracefully
def linux_interaction(): assert ('linux' in sys.platform), "Function can only run on Linux systems." print('Doing something.') try: linux_interaction() except AssertionError as error: print(error) print('The linux_interaction() function was not executed') # running it on windows system will give the following output # Function can only run on Linux systems. # The linux_interaction() function was not executed
What is the difference between raise
and assert
?
raise
allows you to throw an exception at any time.assert
enables you to verify if a certain condition is met and throw an exception if it isn’t.
Don't use assertions as your only line of defence as they can be turned of using python -O main.py
Use exceptions
References¶
Last updated: 2022-11-21