Previous article: A bulleted list of the contents of the Python tutorial (Chapter 7) (under construction)
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.
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/
--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
-** 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.
-** 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.
-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.
-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.
--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.
--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.
--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