[Python for Hikari-] Chapter 07-01 Exception Handling (Errors and Exceptions)

[Python] Chapter 07-01 Errors and Exceptions

I have written various programs so far. To execute the program you wrote yourself, follow the steps below.

** [1] Creating a program ** Take, for example, a program that calculates divisions.

Creating a program is the task of writing code for processing according to the grammar of a program such as Python so that the computer can process something. I have done it many times.

print('Find the division a ÷ b.')
a = int(input('Enter the value of a:'))
b = int(input('Enter a value for b:'))

print(a / b)

Although the above code is made easy for humans to read, it cannot be processed on a computer as it is. To be precise, it is processed by the ** CPU (Central Processing Unit) ** inside the computer, but the above code cannot be executed. The code in this state is sometimes called ** source code **.

** [2] Interpreter processing ** To be able to execute the source code, it must be ** translated **. Specify ** Python interpreter ** to translate. By using the interpreter, you can translate into ** machine language ** that the CPU can understand. Actually, the interpreter is specified in Chapter 01. Let's take a look. Click Edit Configurations ... in the upper right corner of Pycharm. image.png

I specified the part where the interpreter is located in [Python interpreter]. image.png

** [3] Execute line by line ** The program is executed line by line while translating with the interpreter. If there is a grammatical error during program execution, the following error will be displayed as a syntax error, and translation by the interpreter will be stopped. ..

Traceback (most recent call last): File "C:/Users/***/Desktop/python/chap07/samp07-01-01.py", line 6, in a = inat (input ('Please enter the value of a:')) NameError: name 'inat' is not defined

If there are no grammatical mistakes, the program will be executed and the process will proceed.

The above is the flow from writing the program code to executing it.

Exception (division by 0)

I mentioned earlier that if you run the program and there are no syntax errors, the program will be executed, but even if you run the program safely, problems may occur at that time.

For example, the previous program. Create a file with the file name samp07-01-01.py </ font> in chap07 </ font>, and enter the following code Please write. Then try it.

samp07-01-01.py


print('Find the division a ÷ b.')
a = int(input('Enter the value of a:'))
b = int(input('Enter a value for b:'))

print(a / b)

[Execution result] </ font> Find the division a ÷ b. Enter the value of a: 10 Enter a value for b: 4 2.5

I think there is no problem with the contents of the program. By the way, this time, I confirmed that the translation by the interpreter was successful and the execution was successful.

So what if you enter ** 0 ** for the value of ** b ** after execution? Try again and enter ** 0 ** in ** b **.

[Execution result] </ font> Find the division a ÷ b. Enter the value of a: 10 Enter a value for b: 0 </ font> Traceback (most recent call last): File "C:/Users/***/Desktop/python/chap07/samp07-01-01.py", line 5, in print(a / b) ZeroDivisionError: division by zero

What happened is ** "Zero Division Error: division by zero" ** at the end of the error, which means "I'm trying to divide by 0 and I'm getting an error". In fact, even in the world of math and mathematics, values cannot be divided by zero.

Now, the value ** 0 ** you entered is not an error that occurred during translation by the interpreter, but an error ** that occurred after the translation was completed successfully. Actually, it is a human input of ** 0 ** from the execution screen at the time of execution. An error that occurs at runtime, rather than a grammatical error like this, is called an Exception **. Also, syntax errors that occur during translation by the interpreter are called ** syntax errors **.

In summary, there are ** syntax errors ** and ** exceptions ** in the errors. By the way, it is an exception, but even if you are careful not to enter ** 0 ** as the value of ** b ** from the execution screen at the time of execution, people will enter it, so an exception like this one is It always happens somewhere.

Exception (exception when specified beyond the elements of the list)

I mentioned division by 0 earlier, but there are more. For example, when specifying the element number of the list, ** an exception will be thrown if the element number exceeds the number of elements in the list **. Create a new chap07 </ font> and create a file with the file name samp07-01-02.py </ font> in it. Then write the code below.

07-01-02.py


ls = [1, 3, 5, 7, 9]
print(ls)
i = int(input('Please specify the element number in the above list:'))

print(ls[i])

[Execution result] </ font> [1, 3, 5, 7, 9] Specify the element number in the above list: 5 </ font> Traceback (most recent call last): File "C:/Users/***/Desktop/python/chap07/samp07-01-02.py", line 5, in print(ls[i]) IndexError: list index out of range

At the end of the error message is ** "Index Error: list index out of range" **. This is an error message such as "Specifying an index beyond the scope of the list". This time, the list elements should only be ** ls [0] to ls [4] **, but since ** ls [5] ** is specified, it becomes a ** exception **. This time too, an exception is occurring due to human input.

Finally

This time I mentioned exceptions and syntax errors as error types. Examples of exceptions include division by 0 and specification outside the elements of the list. Exceptions due to human error are common. No matter how much you say "Be careful" to people, mistakes will occur. It is dangerous to leave it to others to avoid making such mistakes.

Now, I'll talk about how to write an error-resistant program to avoid this exception next time.

Return to [Table of Contents Link]

Recommended Posts