-3

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Ololo
  • 11
  • 3
  • 3
    Why would you expect a `FileNotFoundError` to happen? Your code makes no attempt to access files. (File access might happen in the functions, but this code doesn't invoke those either.) If you want to raise an exception based on `type_of_model`, put that in another `elif` branch -- don't catch an exception first. – Jeroen Mostert Feb 06 '23 at 13:22
  • Please be sure to tag the language you're using. This looks like Python code so I've added that tag, but please use the "edit" link to change it if I've got that wrong. – T.J. Crowder Feb 06 '23 at 13:23

1 Answers1

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.

bmargulies
  • 97,814
  • 39
  • 186
  • 310