Python try / except notes

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.

Sample code

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)

Execution result

===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

Python try / except notes
Python> try: / except:
python try ~ except ~ else
Try python
Python study notes _000
Python learning notes
Python beginner notes
Python study notes_006
python C ++ notes
Python study notes _005
Python grammar notes
Python Library notes
python personal notes
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
Try scraping with Python.
missingintegers python personal notes
Python package development notes
python decorator usage notes
Python ipaddress package notes
Python Pickle format notes
Try gRPC in Python
[Python] pytest-mock Usage notes
First Python miscellaneous notes
Matlab => Python migration notes
Try 9 slices in Python
Notes around Python3 assignments
Notes using Python subprocesses
Python framework bottle notes
Python notes using perl-ternary operator
O'Reilly python3 Primer Learning Notes
Try to understand Python self
Python standard unittest usage notes
Python notes to forget soon
python * args, ** kwargs Usage notes
Try Python output with Haxe 3.2
Try using Kubernetes Client -Python-
Python notes using perl-special variables
Python 處 處 regular expression Notes
Python Tkinter notes (for myself)
[Python] Notes on data analysis
Try LINE Notify in Python
Python data analysis learning notes
Try running Python with Try Jupyter
Try implementing Yubaba in Python 3
Get Evernote notes in Python
Notes on installing Python on CentOS
Try scraping with Python + Beautiful Soup
Azure Functions: Try Durable Functions for Python
Notes on Python and dictionary types
Try IAM Database Authentication from Python
Python
Minimum grammar notes for writing Python
Try the Python LINE Pay SDK
Try to operate Facebook with Python
Notes on using MeCab from Python
Try implementing extension method in python
Try singular value decomposition with Python
Try using Pleasant's API (python / FastAPI)