英文:
Why exception (try-except Exception) does not work?
问题
以下是您要翻译的代码部分:
def preprocess_corresponds_to_model(type_of_model: str) -> function:
try:
if type_of_model == "X":
preprocessing_function = preprocess_location_df
return preprocessing_function
elif type_of_model == "Y":
preprocessing_function = preprocess_event_df
return preprocessing_function
except FileNotFoundError as exception:
raise Exception(
f"The model {type_of_model} does not exist. "
"The model should be either X or Y"
) from exception
希望这对您有所帮助。
英文:
Here is the code. For some reason, if I have type_of_model, neitehr X, nor Y exception does not work. The exception does not appear.
`def preprocess_corresponds_to_model(type_of_model: str) -> function:
try:
if type_of_model == "X":
preprocessing_function = preprocess_location_df
return preprocessing_function
elif type_of_model == "Y":
preprocessing_function = preprocess_event_df
return preprocessing_function
except FileNotFoundError as exception:
raise Exception(
f"The model {type_of_model} does not exist."
"The model should be either X or Y"
) from exception`
I expect that when as an input parameter I have neither X, nor Y I will see an exception message and my python script will be interrupted. But nothing happens.
答案1
得分: 2
你在异常处理程序的范围内不打开任何文件。你返回一个在该范围之外调用的函数。因此,处理程序无效。
英文:
You do not open any files in the scope of the exception handler. You return a function which is called outside that scope. So the handler is ineffective.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论