WadhwaniAI / covid-modelling

Repo for modelling the spread of novel Covid-19
MIT License
5 stars 0 forks source link

Raising and catching base Exception class #100

Closed jerome-ai closed 3 years ago

jerome-ai commented 3 years ago

There are numerous places where the base Exception class is either raised or caught; a quick search of master:

./data/dataloader/covid19india.py:104:            except Exception:
./data/dataloader/jhu.py:72:            except Exception:
./main/seir/optimisers/base.py:49:            raise Exception('Number of days of prediction cannot be negative.')
./main/seir/uncertainty/abma.py:108:            raise Exception('Validation set cannot be empty')
./main/seir/uncertainty/abma.py:292:            raise Exception(
./scripts/ihme/experiments.py:86:    except Exception as e:
./scripts/seir/batch_runs.py:73:    except Exception as e:
./scripts/seir/experiments.py:80:    except Exception as e:
./utils/fitting/smooth_jump.py:56:            raise Exception("unknown smoothing method provided")
./utils/fitting/smooth_jump.py:76:        raise Exception("unknown smoothing method provided")
./utils/generic/config.py:191:        raise Exception('Undefined pattern')
./utils/generic/config.py:215:                        raise Exception('Insufficient arguments')
./utils/generic/create_report.py:57:    except Exception:
./utils/generic/enums/columns.py:91:        except Exception as e:

In places where it's caught (except), it's dangerous because you have no way of differentiating the error that took place: any exception could have been thrown and it's going to be dealt with in the same way when the catcher probably had a certain error in mind. In places where it's raised (raise), it doesn't give the caller any information about what they should do.

The ideal case would be to have a CovidModellingException class, and a few classes that are children of it that get thrown. An easier case would be to thrown concrete builtin exceptions (ValueError or AttributeError) for example.

sansiddh commented 3 years ago

I have made changes in the same branch. So most of the Exceptions were replaced with ValueError. I created custom exceptions (such as ConfigException, ABMAException) for some of the other Exceptions. Replaced all such Exceptions being raised.