gpt4 book ai didi

using finally clause vs without using finally clause(使用Finish子句与不使用Finish子句)

转载 作者:bug小助手 更新时间:2023-10-25 15:05:09 28 4
gpt4 key购买 nike



What is the difference between this code that uses a finally statement and the code without a finally statement?

使用Finish语句的代码和不使用Finally语句的代码之间有什么不同?


# code 1
a = 10
b = 0

try:
c = a / b
print(c)
except ZeroDivisionError as error:
print(error)
finally:
print('Finishing up.')

and this for code without finally statement, they are looks same. Is there difference such as performance or what?

这对于没有Finish语句的代码来说,它们看起来是一样的。有什么不同吗,比如表现还是什么?


# code 2
a = 10
b = 0

try:
c = a / b
print(c)
except ZeroDivisionError as error:
print(error)
print('Finishing up.') # without using finally statement.

Here is flowchart I got from that site
try-except-finally

这是我从那个网站得到的流程图


In my real case or real application, I'm using OpenCV in frame loop to handle error, but I have no idea shall I use finally or without using finally?

在我的实际案例或实际应用程序中,我在帧循环中使用OpenCV来处理错误,但我不知道应该使用Finally还是不使用Finally?


  while ret:
try:
# Write the preview frame to the video file
video_writer_preview.write(frame)

log_dict[pid] = f'Frame {view_class_dict[pid]} inferencing... '
# Inference
# frame = inference_detect_mtcnn(frame)
if is_dopler:
frame = inference_detect_yoloDopler(frame)
else:
if is_abnclass:
if view_class_dict[pid] == '4CH':
frame = inference_detect_yolo4CH(frame)
elif view_class_dict[pid] == '5CH':
frame = inference_detect_yolo5CH(frame)
elif view_class_dict[pid] == 'LA':
frame = inference_detect_yoloLA(frame)
elif view_class_dict[pid] == 'SA':
frame = inference_detect_yoloSA(frame)
elif view_class_dict[pid] == 'SUB':
frame = inference_detect_yoloSUB(frame)

# Write the processed frame to the video file
video_writer_processed.write(frame)

except Exception as e:
log_dict[pid] = f'Frame inference error! {e}'
print(f"error: {e}")
finally: # shall I use finally clause?
# Read the next frame
ret, frame = capture.read()
progress_dict[pid] += 1

更多回答

stackoverflow.com/a/11552007/4151233

Stackoverflow.com/a/11552007/4151233

finally it will work after finishing of trying the code inside try if there is no errors. but using it , depends on your idea and how you want to implement it

最后,如果没有错误,它将在TRY内的代码尝试完成后工作。但使用它,取决于你的想法和你想要如何实现它

优秀答案推荐
更多回答

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com