The contents of the Python tutorial (Chapter 8) are itemized.

Previous article: A bulleted list of the contents of the Python tutorial (Chapter 7) (under construction)

Introduction

Python3 Engineer Certification Basic Exam As a countermeasure, this is a personal memo that summarizes the contents of the Python tutorial (book) in easy-to-memorize bullet points.

Reference material

Python Tutorial: https://docs.python.org/ja/3/tutorial/ Chapter 8: https://docs.python.org/ja/3/tutorial/errors.html Books: https://www.oreilly.co.jp/books/9784873117539/

"Chapter 8 Errors and Exceptions"

--Python3 Engineer Certification Basic Exam Score ―― 4/40 questions (10.0%) ☆☆ ★★★ (Importance: Medium-) --Theme --Syntax error --Exceptions (built-in exceptions and user-defined exceptions) --Exception handling (try, except, else, finally) --raise statement

8.1 Syntax error

-** SyntaxError ** is detected by the ** parser ** before execution. --The parser uses a small arrow to indicate where the syntax error was first found. --The cause of the error is the token before the arrow. --The file name and line number are also displayed.

8.2 Exception

-** exception ** is detected during execution. There are the following two types. -** Built-in exceptions ** ... Built-in exceptions provided by Python. -** User-defined exceptions ** ... User-defined exceptions. --There are various types of exceptions, and the types are described in the message. --In the built-in exception, the type and the name in the message always match. --Matching is optional for user-defined exceptions. --Most of the exceptions are not handled by the program and the result appears in the error message. --The first half of the error message shows the context in which the exception occurred in the form of a stack traceback. --Do not display lines read from standard input. --The last line of the error message describes what happened. --Details of the exception type and cause are given later in the line.

8.3 Handling exceptions

-The ** try statement ** allows you to handle the selected exception.
The try statement consists of the following. The try and except clauses are required, and the else and finally clauses can be omitted. -** try clause **
Block under try :. Write at the top of the try statement.
Write the process that may throw an exception. -** except clause **
except [exception name] [as variable name]: Block under. Write below the try clause.
Write the processing when an exception is thrown. --It is also possible to write multiple except clauses. --If you omit the exception name, you can catch all exceptions. --as If you enter a variable name, an instance of the exception will be assigned to the variable. --Arguments are stored in the ** args attribute . --Since ** \ _ \ _ str () \ _ \ _ method ** is defined in the exception instance, it can be displayed directly by print (instance name) without referring to the args attribute. --By enumerating exceptions in tuples, it is possible to catch multiple exceptions in one except clause. - else clause **
Block under else :. Write below the except clause.
Write the process when an exception is not thrown in the try clause. -** finally clause **
The block under finally :. Write at the bottom of the try statement.
[Cleanup process] at the end of the try statement (https://qiita.com/Wakii/items/4ffcba348e31e7e141fc#86-%E3%82%AF%E3%83%AA%E3%83%BC%E3 % 83% B3% E3% 82% A2% E3% 83% 83% E3% 83% 97% E5% 8B% 95% E4% BD% 9C% E3% 81% AE% E5% AE% 9A% E7% BE Write% A9). --The execution order of try statements is as follows. --The try clause is executed first. --If no exception is thrown in the try clause, the except clause is skipped. --If there is an else clause, execute it. --If an exception is thrown in the try clause, the rest of the try clause and the else clause are skipped. --If there is an except clause that matches the exception, execute it. --If there is no except clause that matches the exception, the exception is re-thrown to the upper try statement if there is one. --If there is a finally clause, it will be executed before resending. --If there is no upper try statement, ** unhandled exception ** message is displayed and processing ends. --Exceptions that occur in a function called directly or indirectly from the try clause are also processed in the except clause. --If there is a finally clause, execute it regardless of whether an exception is thrown or not and end the try statement.

8.4 Throwing an exception

-The ** raise statement ** can be used to forcibly throw an exception specified in the program. --Write in the form of raise [exception]. --The specified exception can be an exception class or an exception instance. --If you write only raise without specifying an argument in the except clause, the caught exception is re-thrown.

8.5 User-defined exceptions

--You can also create and handle your own exception classes. --The exception class should normally be a class derived from the ** Exception class **. --The name of the exception class should be "~ Error", just like standard exceptions. --Like a normal class, you can override methods in an exception class, but you don't usually do complicated things. --When writing a module that throws various errors, it is common to first define the base class of the exception and treat individual errors as their subclasses.

8.6 Definition of cleanup behavior

--Cleanup processing is finally clause (-> [8.3 Exception handling](https://qiita.com/Wakii/items/4ffcba348e31e7e141fc#83-%E4%BE%8B%E5%A4%96%E3%81% AE% E5% 87% A6% E7% 90% 86))). --Used for processing that you want to execute regardless of the presence or absence of exceptions, such as releasing external resources. --If there is an exception that is not supported in the except clause, execute the processing of the finally clause and then re-send it. --The finally clause is also executed when exiting from other clauses in the try statement with break, continue, retuen, etc.

8.6.1 Cleanup behavior defined on an object

--Some objects have defined cleanup actions when they are no longer needed. --If you use ** with statement ** to process an object, Python will clean it up in a proper way immediately after use.

Next article: A bulleted list of the contents of the Python tutorial (Chapter 9) (under construction)

Recommended Posts

The contents of the Python tutorial (Chapter 5) are itemized.
The contents of the Python tutorial (Chapter 4) are itemized.
The contents of the Python tutorial (Chapter 8) are itemized.
The contents of the Python tutorial (Chapter 1) are itemized.
The contents of the Python tutorial (Chapter 10) are itemized.
The contents of the Python tutorial (Chapter 6) are itemized.
The contents of the Python tutorial (Chapter 3) are itemized.
Get the contents of git diff from python
the zen of Python
Template of python script to read the contents of the file
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
[Maya Python] Crush the contents of the script 2 ~ list Notes
Reproduce the execution example of Chapter 5 of Hajipata in Python
Towards the retirement of Python2
About the ease of Python
About the features of Python
Simulation of the contents of the wallet
The Power of Pandas: Python
About the matter that the contents of Python print are not visible in docker logs
[Maya Python] Crush the contents of the script 3 ~ List unknown Plugins
[Maya Python] Crush the contents of the script 1 ~ Camera Speed Editor
[Data science memorandum] Confirmation of the contents of DataFrame type [python]
A Python script that compares the contents of two directories
How to check if the contents of the dictionary are the same in Python by hash value
Easy encryption of file contents (Python)
[Python] The stumbling block of import
[Python of Hikari-] Chapter 09-03 Class (inheritance)
First Python 3 ~ The beginning of repetition ~
[Python] [Table of Contents Links] Python Programming
Understand the contents of sklearn's pipeline
[Translation] scikit-learn 0.18 Tutorial Table of Contents
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
Change the Python version of Homebrew
See the contents of Kumantic Segumantion
Python Math Series ⓪ Table of Contents
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Introductory table of contents for python3
About the basics list of Python basics
Learn the basics of Python ① Beginners
[python, ruby] fetch the contents of a web page with selenium-webdriver
Output the contents of ~ .xlsx in the folder to HTML with Python
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
Verification of the theory that "Python and Swift are quite similar"
Contents of reading VBA x Python fastest work technique Memo chapter1
[Python] A program that rotates the contents of the list to the left
I checked the contents of docker volume
Change the length of Python csv strings
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
[Python] Understand the content of error messages
Python tutorial
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Python3] Rewrite the code object of the function
I didn't know the basics of Python
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same