Python Exception Handling

Programming In Python 3 Second Edition page 172
import logging
import warnings
_logger = logging.getLogger(__name__)""" it is custom exception """
class BusinessException(Exception):
"""
it is constructor of custom exception
:param code: code of message exception
:param message: exception message and frontend modal content
"""
def __init__(self, code, message):
super().__init__(message)
self.code = code
self.message = message
def get_message(self):
return self.message
def get_code(self):
return self.code
import logging
import warnings
_logger = logging.getLogger(__name__)
from exception_util.custom_exception import BusinessException
def exception_test(param):
if param == 1:
raise BusinessException("123","custom error exception")
if param == 2:
raise Exception("exception")
def main():
print("start")
try:
exception_test(1)
except BusinessException as customErr:
_logger.error(customErr.get_message())
except Exception as customErr:
_logger.error(customErr)
finally:
print("finally")
if __name__ == "__main__":
main()
try:except:finally:

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

12 Reasons Why You Won’t Want To Miss Update 12

Back up and restore Mongo DB(hosted on AWS EC2) with a shell script, cron, and AWS S3

CI/CD (Pipeline) Clarified

What Is the Application Development Lifecycle?

Creating Collectables in Unity

Why is Python becoming indispensable in IoT Industry?

What is Power BI?

ApeDubai Partners With Bitxmi To Host AMA Session.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
wahyu eko hadi saputro

wahyu eko hadi saputro

i am a reader

More from Medium

[Python] Decorator

Python timeit module

Python logging basicConfig

Python Encapsulation