Pharmaceutical company researchers summarized Python exception handling

Introduction

This section describes Python exception handling.

Exception handling basics

Exception handling describes what to do if an error occurs while the program is running. Using try and ʻexcept`, write as follows.

import pandas as pd

try:
    df = pd.read_csv('sample.csv')
except FileNotFoundError:
    print('File not found.')

Describe the process that may cause an error (exception) after try, and the process when an error occurs after ʻexcept. If you write the error type after ʻexcept, exception handling will be executed when the specified error occurs. In the example shown here, the processing of the ʻexceptclause is executed only whenFileNotFoundError (exception that the specified file cannot be found) occurs. If nothing is described immediately after ʻexcept, the processing of the ʻexcept` clause will be executed for all errors, but it will also be processed for unexpected errors, so it is recommended. It will not be.

else clause

If you enter ʻelse`, you can describe the process you want to continue if no error occurs.

import pandas as pd

try:
    df = pd.read_csv('sample.csv')
except FileNotFoundError:
    print('File not found.')
else:
    df.to_excel('sample.xlsx', index=False)

In the above example, the same contents as the read file are saved as an Excel file.

finally clause

You can use finally to describe what you want to do regardless of whether an error has occurred.

import pandas as pd

try:
    df = pd.read_csv('sample.csv')
except FileNotFoundError:
    print('File not found.')
finally:
    print('finished.')

In the above example, the string finished. is output at the end regardless of whether the file can be read or not.

Summary

Here, I explained about exception handling in Python. It is a good idea to include exception handling when requesting input from the user or connecting to the database.

Recommended Posts

Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized functions in Python
Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized scikit-learn
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Python exception handling
Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized Python's data structures
Python, about exception handling
How to install Python for pharmaceutical company researchers
Python exception handling (Python learning memo ⑥)
Exception handling during Python API communication
A pharmaceutical company researcher summarized the basic description rules of Python
Exception handling
I tried to summarize Python exception handling
[Introduction to Udemy Python3 + Application] 65. Exception handling
Python exception handling a little more convenient
Module import and exception handling in python
Python Error Handling
boto3 exception handling
Python timezone handling
Summary of Python articles by pharmaceutical company researcher Yukiya
[Python for Hikari-] Chapter 07-01 Exception Handling (Errors and Exceptions)
Handling yaml with python
Handling json in python
Exception message in Python
Python decimal point handling
Hexadecimal handling in Python 3
Python exception class list
Fizzbuzz with exception handling