Main function Sub function and Exception

Asked 2 years ago, Updated 2 years ago, 55 views

Hello.

I have a question while working on the Python code, but I'm uploading it here because it's not told in textbooks or lectures. I need your help.

As shown below, func1 and func2 are all surrounded by try and exception, and if an error occurs in func2 when func1 calls func2,

1) Only the 'error in func2' error message is output. Why is the exception of the main function not processed?

2) Finally: Is it normal for both 'end of func1' and 'end of func2' to be printed?

def func1():

try:
    func2()

exception:
    print('error in func1')

finally:
    print('end of func1')

def func2():

try:
    r= request.get('http://naver.com')

exception:
    print('error in func2')

finally:
    print('end of func2')

exception

2022-09-22 18:10

1 Answers

If you want to pass an exception to the caller, use raise.

https://docs.python.org/ko/3/reference/simple_stmts.html#the-raise-statement


2022-09-22 18:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.