Skip to content

Use correct exceptions #19212

Closed
Closed

Description

@iSazonov

Summary of the new feature / enhancement

In PowerShell code base there are some places where we are not accurate with exceptions.
Typical example:

internal void Rename(string newName)
{
if (string.IsNullOrEmpty(newName))
{
throw new ArgumentNullException(nameof(newName));
}
Name = newName;
}

Here we throw ArgumentNullException that is not correct if the argument is empty. (There are examples with other exceptions.)

My proposal is to fix such code and throw ArgumentNullException if argument is null and ArgumentException is argument is empty.

This is not even a breaking change, as it is not a functional exception, and it is worth correcting it for the correct one. If someone even uses this behavior in their code, it is catastrophically bad code.

The proposal comes from the fact that we are blocked from using new .Net API like ArgumentException.ThrowIfNullOrEmpty(). The API makes code more friendly for .Net Rumtime optimizations (like inlining). We already use this kind of API in a lot of places and it's worth updating the rest.

Proposed technical implementation details (optional)

No response

Activity

  1. added
    Issue-Enhancementthe issue is more of a feature request than a bug
    Review - MaintainerThe PR/issue needs a review from the PowerShell repo Maintainers
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Feb 24, 2023
  2. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    and removed
    Review - MaintainerThe PR/issue needs a review from the PowerShell repo Maintainers
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Feb 24, 2023
  3. daxian-dbw commented on Feb 24, 2023

    @daxian-dbw
    Member

    There is no argument around using ArgumentException.ThrowIfNullOrEmpty() when it's applicable, and #19197 was already merged in, which does exactly this kind of changes.

    So, removing the Review - Maintainer label, and marking Up-for-Grabs.

  4. iSazonov commented on Feb 24, 2023

    @iSazonov
    CollaboratorAuthor

    NullOrEmpty is the most common, but there are others (ex., where Count or Length == 0). (Better to search by throw new Argument pattern.)

  5. daxian-dbw commented on Feb 24, 2023

    @daxian-dbw
    Member

    It's better not mix all together.

    • For cases where the existing ThrowIfXXX APIs fit properly, it's generally fine to accept the change.
    • For cases where the existing ThrowIfXXX APIs don't fit, for example, when checking if a collection is null or empty, or when checking if an argument or one of its members is null, we should not apply the ThrowIfXXX APIs blindly. For those cases, please use a different issue to describe the intended changes, for maintainers to review.
  6. xtqqczze commented on Feb 24, 2023

    @xtqqczze
  7. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  8. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  9. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  10. microsoft-github-policy-service commented on Nov 22, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions