Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality] Add rector: CatchThrowableInsteadOfExceptionRector #6644

Closed
wants to merge 2 commits into from

Conversation

pboos
Copy link

@pboos pboos commented Jan 3, 2025

This adds a rector that in try catch blocks replaces the last } catch (Exception ...) { with } catch (Throwable ...) {
Reason for this is that Exception doesn't catch all the problems like TypeError which often want to be caught in such try catch blocks.

Example:

try {
	$this->doSomething();
} catch (Exception $exception) {
	// do something
}

becomes

try {
	$this->doSomething();
} catch (Throwable $exception) {
	// do something
}

@samsonasik
Copy link
Member

Rector is not error handler, so the rule is not expected in the core imo :)

Feel free to create your custom rule for your usecase/project.

@samsonasik
Copy link
Member

I am closing this as Rector is not supposed to be used for error handler, but feel free to create your own custom rule for your use case.

Thank you for understanding.

@samsonasik samsonasik closed this Jan 3, 2025
@pboos
Copy link
Author

pboos commented Jan 3, 2025

@samsonasik As you wish 😃. Just thought it might benefit others. We already use this as a custom rector rule. So for me not a problem.

I personally thought this could benefit others as a simple way to find and replace such occurrences that can lead to problems. As also ArithmeticError is not an exception and some people accidentally use Exception instead of Throwable for a "catch all".

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