While playing with the source code of the web application, I encountered the following situation.
--When you execute a certain function, you want to continue the process by passing only the error caused by the specific process. --There is a process that you want to be executed regardless of success or failure. --I want to notify the front desk that some errors have occurred during processing.
I wrote it like this in python's try / catch and it worked, so I'll write it down as a memorandum.
class SampleException(Exception):
pass
def specific_func(raise_error: bool):
if raise_error:
raise Exception("error from specific_func")
else:
print("[INFO] success specific_func")
def error_sample(raise_error: bool):
try:
specific_func(raise_error=raise_error)
except Exception as e:
print("[ERROR] ",e)
raise SampleException("Some processing failed")
finally:
print("[INFO]The process you want to execute")
main.py
print("===Abnormal system=====================")
try:
error_sample(raise_error=True)
print("(notification)Successful processing")
except SampleException as e:
print("(notification)", e)
print("===Normal system=====================")
try:
error_sample(raise_error=False)
print("(notification)Successful processing")
except SampleException as e:
print("(notification)", e)
===Abnormal system=====================
[ERROR] error from specific_func
[INFO]The process you want to execute
(notification)Some processing failed
===Normal system=====================
[INFO] success specific_func
[INFO]The process you want to execute
(notification)Successful processing
Recommended Posts