Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork495
Description
Use-case:
Minimal example:
defrun_ga(**kwargs):try:ga=pygad.GA(**kwargs)ga.run()returngaexceptExceptionase:my_custom_logger.print(f"GA failed with kwargs ={kwargs} with exception{e}")returnNonemy_ga=run_ga(num_generations=-1)
Desired behavior:
Program runs, and logs an exception with details about the error - i.e. thatnum_generations has to be positive.
Current implementation:
With the currentsys.exit(-1) inGA.__init__, this willnot be caught by theexcept block. Even if we change theexcept Exception toexcept SystemExit (which nobody should ever do), then it would log only theSystemExit with no details intomy_custom_logger.
Proposed change:
In theGA.__init__, we can replace the end of the function. Instead of the current:
exceptExceptionase:self.logger.exception(e)sys.exit(-1)
We can rather use:
exceptExceptionase:self.logger.exception(e)raisee
This way, the exception will be logged properly intoself.logger, but then it will not kill the whole python process, but rather re-raise the exception. The caller ofpygad.GA() can then decide whether this exception will be left uncaught and will exit the program, or they will catch it and act upon it.