Last active
October 20, 2023 06:26
-
-
Save FluffyDietEngine/a4dc19967616593e0a1b7f67b93edf11 to your computer and use it in GitHub Desktop.
exception handler - advalearn 20 Oct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
1. Create a function for addition | |
2. expect an error | |
3. Handle the error | |
4. write a condition if there is no error | |
5. write a block of code if there is no error | |
''' | |
import logging | |
logger = logging.getLogger() | |
logger.setLevel(logging.DEBUG) | |
stream_handler = logging.StreamHandler() | |
logger.addHandler(stream_handler) | |
def addition(a: int, b: int) -> int: | |
"""_summary_ | |
Args: | |
a (int): _description_ | |
b (int): _description_ | |
Returns: | |
int: _description_ | |
""" | |
c = 0 | |
try: | |
c = a+b | |
logger.info(f"Addition is success, result -> {c=}") | |
except TypeError: | |
try: | |
addition(int(a), b) | |
except ValueError: | |
logger.exception("Character cannot be converted to integer") | |
logger.exception(f"error happened, a-> {type(a)}, b-> {type(b)}") | |
else: | |
logger.info("No exception happened") | |
finally: | |
logger.info("All is well") | |
c+=1 | |
return c | |
print(addition("a", 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment