Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Throw exception instead of sys.exit in GA.__init__ #213

Closed
Labels
enhancementNew feature or request
@holasjuraj

Description

@holasjuraj

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp