[PYTHON] If an exception occurs in the function, it will be transmitted to the caller 2

1


def test_exception(num):
    print(1)
    try:
        print(2)
        answer = 100 / num
        return answer
        print(3)
    except ZeroDivisionError as e:
        print(4)
        raise e
    print(5)

print('start')
try:
    test_exception(1)
    print(6)
except ZeroDivisionError as e:
    print(7)
    raise e
finally:
    print('end')

Execution result of 1


start
1
2
6
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 Execute in the try block in the test_exception function. Execute print (2) and answer = 100 And returns 100. Because it is returning Note that print (3) below it is not executed. Since ZeroDivisionError has not occurred The except block in the test_exception function is not executed and is skipped.

Now that the test_exception function of the try block is finished Then execute print (6).

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)
    try:
        print(2)
        answer = 100 / num
        return answer
        print(3)
    except ZeroDivisionError as e:
        print(4)
        raise e
    print(5)

print('start')
try:
    test_exception(0)
    print(6)
except ZeroDivisionError as e:
    print(7)
    raise e
finally:
    print('end')

Execution result of 2


start
1
2
4
7
end

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
If you remove the list to be looped, you will get terrible.
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.)
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 33 --Python --Basics of the Python language 8
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.
Check if it is Unix in the scripting language
Determine if an attribute is defined in the object
Check if it is Unix in the scripting language
Kaggle Tutorial Titanic know-how to be in the top 2%
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
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] How to check if the Key exists in the dictionary
[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
If it is not easy to understand, it cannot be improved.
How to check if a value exists in an enum
In omegaconf, let's pass the direct parameter file to the function
I tried to implement the mail sending function in Python
An introduction to the modern socket API to learn in C
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