Skip to content

fix: replace 9 bare except clauses with except Exception#2772

Open
KeloYuan wants to merge 1 commit into
gee-community:masterfrom
KeloYuan:fix/bare-except-improvements
Open

fix: replace 9 bare except clauses with except Exception#2772
KeloYuan wants to merge 1 commit into
gee-community:masterfrom
KeloYuan:fix/bare-except-improvements

Conversation

@KeloYuan

@KeloYuan KeloYuan commented May 8, 2026

Copy link
Copy Markdown

Replaces 9 bare except: with except Exception: in 6 files: timelapse.py, map_widgets.py, conversion.py, coreutils.py, common.py, geemap.py.

Changed across 6 files: timelapse.py, map_widgets.py, conversion.py, coreutils.py, common.py, geemap.py
Copilot AI review requested due to automatic review settings May 8, 2026 16:17

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces bare except clauses with except Exception across multiple modules, including common.py, conversion.py, coreutils.py, geemap.py, map_widgets.py, and timelapse.py. The reviewer recommends further refining these handlers by catching specific exceptions—such as KeyError, ValueError, IndexError, TypeError, and ImportError—to avoid masking unrelated logic errors and to improve the precision of error handling.

Comment thread geemap/common.py
percentile_2 = min([stats[s]["percentile_2"] for s in stats])
percentile_98 = max([stats[s]["percentile_98"] for s in stats])
except:
except Exception:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The try block performs dictionary lookups and calls min()/max() on list comprehensions. These operations can raise KeyError (if a key is missing) or ValueError (if the list is empty). Catching these specific exceptions is preferred over the broad Exception class to avoid masking unrelated logic errors.

except (KeyError, ValueError):

Comment thread geemap/conversion.py
if (line.replace(" ", ""))[line.find("!") + 1] != "=":
line = line.replace("!", "not ")
except:
except Exception:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The string indexing logic [line.find("!") + 1] is prone to an IndexError if the '!' character is at the end of the string or if the resulting string is empty. It is better to catch IndexError specifically rather than the broad Exception class.

except IndexError:

Comment thread geemap/coreutils.py
try:
toolbar_button = ipywidgets.ToggleButton(**widget_args)
except:
except Exception:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The failure here is likely due to an unsupported keyword argument in ipywidgets.ToggleButton (such as layout in older versions), which raises a TypeError. Catching TypeError specifically is more precise for this version-compatibility check.

except TypeError:

Comment thread geemap/geemap.py
try:
size = int(font_size.replace("pt", ""))
except:
except Exception:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since the try block specifically targets a potential failure in int() conversion, catching ValueError is more precise than catching the base Exception class. This is especially relevant here as you are re-raising a ValueError with a custom message.

except ValueError:

Comment thread geemap/timelapse.py
try:
from PIL import Image, ImageDraw, ImageFont, ImageSequence
except:
except Exception:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When handling optional dependency imports, it is best practice to catch ImportError specifically. Catching the broad Exception class can mask other unexpected errors that might occur during module initialization.

Suggested change
except Exception:
except ImportError:

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves exception-handling hygiene across geemap by replacing 9 bare except: clauses with except Exception: in several modules, reducing the chance of unintentionally catching BaseException subclasses (e.g., KeyboardInterrupt, SystemExit).

Changes:

  • Replaced bare except: with except Exception: in 6 files (9 sites total).
  • Preserved existing fallback behaviors while avoiding overly-broad BaseException catches.
  • Standardized exception syntax in several utility and widget-related helpers.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
geemap/timelapse.py Updates PIL import guard to avoid bare except:.
geemap/map_widgets.py Updates color normalization conversion guard to avoid bare except:.
geemap/geemap.py Updates font_size parsing guard in add_labels() to avoid bare except:.
geemap/coreutils.py Updates widget construction fallbacks to avoid bare except:.
geemap/conversion.py Updates JS-to-Python conversion operator handling guard to avoid bare except:.
geemap/common.py Updates several compatibility/stat-parsing fallbacks to avoid bare except:.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread geemap/timelapse.py
Comment on lines +30 to 31
except Exception:
pass
Comment thread geemap/map_widgets.py
Comment on lines +417 to 418
except Exception:
raise ValueError(f"Unable to convert rgb value to hex: {color}")
Comment thread geemap/geemap.py
Comment on lines +4375 to 4376
except Exception:
raise ValueError("font_size must be something like '10pt'")
Comment thread geemap/coreutils.py
Comment on lines 547 to 552
try:
toolbar_button = ipywidgets.ToggleButton(**widget_args)
except:
except Exception:
widget_args.pop("layout")
toolbar_button = ipywidgets.ToggleButton(**widget_args)
toolbar_button.layout.width = "28px"
Comment thread geemap/coreutils.py
Comment on lines 556 to 561
try:
close_button = ipywidgets.ToggleButton(**close_button_args)
except:
except Exception:
close_button_args.pop("layout")
close_button = ipywidgets.ToggleButton(**close_button_args)
close_button.layout.width = "28px"
Comment thread geemap/conversion.py
Comment on lines +610 to 611
except Exception:
print("continue...")
Comment thread geemap/common.py
percentile_2 = min([stats[s]["percentile_2"] for s in stats])
percentile_98 = max([stats[s]["percentile_98"] for s in stats])
except:
except Exception:
Comment thread geemap/common.py
try:
cmap = plt.get_cmap(cmap, k)
except:
except Exception:
Comment thread geemap/common.py
try:
cmap = plt.get_cmap(cmap_name, n_class)
except:
except Exception:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants