Skip to content

Replace BaseException with Exception across codebase#8859

Merged
ericspod merged 2 commits into
Project-MONAI:devfrom
AlexanderSanin:fix/remove-base-exception-7401
May 19, 2026
Merged

Replace BaseException with Exception across codebase#8859
ericspod merged 2 commits into
Project-MONAI:devfrom
AlexanderSanin:fix/remove-base-exception-7401

Conversation

@AlexanderSanin
Copy link
Copy Markdown
Contributor

@AlexanderSanin AlexanderSanin commented May 18, 2026

Summary

Fixes #7401

  • Replaced all 17 occurrences of except BaseException with except Exception across 13 files in monai/ and tests/
  • Catching BaseException inadvertently suppresses KeyboardInterrupt, SystemExit, and GeneratorExit, which should nearly always propagate
  • Exception is the appropriate base class for catchable errors in all these contexts

Files changed

  • monai/__init__.py
  • monai/config/deviceconfig.py
  • monai/utils/tf32.py
  • monai/inferers/inferer.py
  • monai/data/__init__.py
  • monai/apps/auto3dseg/utils.py
  • monai/apps/auto3dseg/ensemble_builder.py
  • monai/apps/auto3dseg/data_analyzer.py
  • monai/apps/detection/metrics/coco.py
  • monai/apps/nnunet/nnunetv2_runner.py
  • tests/test_utils.py
  • tests/networks/nets/test_resnet.py
  • tests/apps/detection/networks/test_retinanet.py

Test plan

  • All existing unit tests should pass without modification (this is a strict narrowing of catch scope)
  • Verify KeyboardInterrupt (Ctrl+C) is no longer silently swallowed during long-running operations

Signed-off-by: Oleksandr Sanin alexaaander.sanin@gmail.com

)

Catching BaseException inadvertently suppresses KeyboardInterrupt,
SystemExit, and GeneratorExit, which should nearly always propagate.
All 17 occurrences across monai/ and tests/ are replaced with
Exception, which is the appropriate base class for catchable errors.

Signed-off-by: Oleksandr Sanin <alexaaander.sanin@gmail.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

📝 Walkthrough

Walkthrough

This PR systematically narrows exception handling from BaseException to Exception across 13 library files and 4 test files. The change prevents control-flow exceptions (KeyboardInterrupt, SystemExit) from being caught and suppressed by import-time fallbacks, GPU-to-CPU execution fallbacks, metric computation handlers, and utility initialization blocks. Each change is identical in pattern: a single exception type parameter in an except clause.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 53.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately and concisely summarizes the main change: replacing BaseException with Exception across the codebase.
Linked Issues check ✅ Passed All changes directly address #7401 requirements: 17 occurrences of BaseException replaced with Exception across 13 files to prevent suppression of KeyboardInterrupt and SystemExit.
Out of Scope Changes check ✅ Passed All changes are narrowly scoped to the stated objective; only exception handler specifications were modified with no unrelated alterations.
Description check ✅ Passed PR description fully matches the template with all required sections completed: summary, file list, types of changes, and test plan.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

…@gmail.com>

I, Oleksandr Yizchak Sanin <alexaaander.sanin@gmail.com>, hereby add my Signed-off-by to this commit: e3a2bb2

Signed-off-by: Oleksandr Yizchak Sanin <alexaaander.sanin@gmail.com>
Copy link
Copy Markdown
Member

@ericspod ericspod left a comment

Choose a reason for hiding this comment

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

Hi @AlexanderSanin thanks for this update, it looks good to me. I went through the changes and don't see anywhere we needed to catch subtypes of BaseException such as KeyboardInterrupt.

@ericspod ericspod merged commit 286e905 into Project-MONAI:dev May 19, 2026
24 checks passed
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.

Remove BaseException Uses

2 participants