If an exception occurs in the function, it will be transmitted to the caller 1

1


def test_exception(num):
    print(1)
    answer = 100 / num
    return answer
    print(2)
    
print('start')
try:
    test_exception(1)
    print(4)
except ZeroDivisionError as e:
    print(3)
    print(e)
finally:
    print('end')

Execution result of 1


start
1
4
end

print ('start') Run first.

next, Execute a try block. Enter the test_exception function on the first line of the try block with argument 1. So Execute print (1) and answer = 100 And returns 100. Because it is returning Note that print (2) below it is not executed. Now that the test_exception function of the try block is finished Then execute print (4).

Since ZeroDivisionError has not occurred The except block is not executed and is skipped.

Finally, execute the finally block.

If the argument of the test_exception function is 0,

2


def test_exception(num):
    print(1)
    answer = 100 / num
    return answer
    print(2)
    
print('start')
try:
    test_exception(0)
    print(4)
except ZeroDivisionError as e:
    print(3)
    print(e)
finally:
    print('end')

Execution result of 2


start
1
3
division by zero
end

print ('start') Run first.

next, Execute a try block. Enter the test_exception function on the first line of the try block with 0 arguments. So Execute print (1) and An error occurs because the answer divides 100 by 0. Again, Because it is returning Note that print (2) below it is not executed. An error occurred in the test_exception function of the try block, so The continuation is not executed.

Since ZeroDivisionError has occurred Execute the except block.

Finally, execute the finally block.

Recommended Posts

If an exception occurs in the function, it will be transmitted to the caller 2
If an exception occurs in the function, it will be transmitted to the caller 1
If you want to put an argument in the closure function and execute it later
[Python] I want to know the variables in the function when an error occurs!
Understand Python yield If you put yield in a function, it will change to a generator
For the first time in Numpy, I will update it from time to time
[Python] What to do if an error occurs in pip (pyinstaller, pyautogui, etc.)
If you add sudo on ubuntu, it will be called the default python.
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
I tried to predict the horses that will be in the top 3 with LightGBM
If you write the View decorator in urls.py in Django, the list will be higher.
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
What to do if a version error occurs in the selenium Chrome driver
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
Get the caller of a function in Python
In bash, "Delete the file if it exists".
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
What to do when is not in the sudoers file.This incident will be reported.
What to do if the print command itself causes an error in Maya python
What to do if a UnicodeDecodeError occurs in pip
Determine if an attribute is defined in the object
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
What to do if ʻObject arrays cannot be loaded when allow_pickle = False` occurs in numpy.load ()
An easy way to view the time taken in Python and a smarter way to improve it
If the character you are trying to read does not match the conversion specification, the C> scanf ()> scanf function will leave the character unread in the stream.
You will be an engineer in 100 days --Day 63 --Programming --Probability 1
You will be an engineer in 100 days --Day 27 --Python --Python Exercise 1
You will be an engineer in 100 days --Day 65 --Programming --Probability 3
[Python] It might be useful to list the data frames
You will be an engineer in 100 days --Day 64 --Programming --Probability 2
What to do if the package dependency cannot be repaired
You will be an engineer in 100 days --Day 34 --Python --Python Exercise 3
[Python] How to write an if statement in one sentence.
How to manipulate the DOM in an iframe with Selenium
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
If it is not easy to understand, it cannot be improved.
You will be an engineer in 100 days --Day 31 --Python --Python Exercise 2
I wrote it in Go to understand the SOLID principle
In omegaconf, let's pass the direct parameter file to the function
I tried to implement the mail sending function in Python
Check the Check button in Tkinter to allow Entry to be edited
How to get rid of the "Tags must be an array of hashes." Error in the qiita api
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
I made a function to check if the webhook is received in Lambda for the time being
If you write go table driven test in python, it may be better to use subTest