Skip to content

Inappropriate use of Win32Exception #26227

Closed as not planned
Closed as not planned

Description

@tdinucci

If you look at:

https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs

You'll see that there are many places where Win32Exceptions are thrown and this doesn't feel right on non-Win32 platforms.

There probably is no great way to address this since moving away from Win32Exception would break existing applications. I wonder though if it'd be an improvement if a new ProcessException type was introduced which descended from Win32Exception? This way existing applications could continue to handle Win32Exceptions but developers could choose to handle ProcessExceptions when developing on non-Win32 platforms.

Activity

  1. stephentoub commented on May 20, 2018

    @stephentoub
    Member

    It's an unfortunate name, but it works cross-platform, and is really just a PlatformException with a less-than-ideal name. Changing it would be a huge breaking change, and when we previously discussed it, we decided there wasn't enough value in introducing a new exception.

  2. tdinucci commented on May 20, 2018

    @tdinucci
    MemberAuthor

    The value I see here is primarily from a non-developers perspective, e.g. admins looking at logs, end users who see raw exception messages in some default exception handler, etc. Also, there'll be some value in the perception that non-Windows platforms are considered first class citizens.

    I totally get that resolving this by doing something like renaming Win32Exception or creating a brand new UnixException (which didn't descend from Win32Exception would be a massive breaking change).

    With my suggestion though (i.e. subclass Win32Exception) the potential to break existing code is significantly reduced (at the expensive of having a slightly confused type hierarchy). We're really talking about breaking places where types are compared explicitly, e.g.

    catch(Exception ex)
    {
    	if(ex.GetType() == typeof(Win32Exception))
    		...
    }

    I'd hope code like the above is rare but it would obviously break. However, I'd imagine this could be mitigated by the compiler spitting out a warning on non-Windows machines. There's obviously a question over what would happen with assemblies compiled and tested against an older version of these libraries compared to what it's actually executing on.

    Anyway, I don't want to re-open a debate that's already been had. I just want to make sure that my original suggestion's been interpreted as intended.

  3. danmoseley commented on May 20, 2018

    @danmoseley
    Contributor

    Im inclined to agree with @tdinucci that it would make code clearer (it always catches my eye on cross platform code). We historically consider replacing an exception with a derived type as very low risk.

    It wouldn't help with the HResult property though. But that's less commonly seen in cross plat code.

  4. jnm2 commented on May 28, 2018

    @jnm2
    Contributor

    Plus, HResult is defined on the base Exception class.

  5. asyncawaitmvvm commented on Jul 6, 2018

    @asyncawaitmvvm

    Oh you guys.

    if (string.IsNullOrEmpty(filename))
    {
        throw new Win32Exception(Interop.Error.ENOENT.Info().RawErrno);
    }
    

    What exactly IS this garbage?

    P.S. ENOENT = no file or directory found.

    Yet under System.IO we have:

    FileNotFoundException
    DirectoryNotFoundException

    Don't justify this source file, fix it.

  6. danmoseley commented on Jul 7, 2018

    @danmoseley
    Contributor

    @asyncawaitmvvm is catching it making your code less readable, or you just noticed this in corefx code?

    Would ProcessException derived from Win32Exception be a substantial improvement for you?

  7. bgribaudo commented on Aug 15, 2019

    @bgribaudo
    Contributor

    A similar idea occurred to me when working on adding dotnet/corefx#34147. Would be nice to see something like this. :-)

  8. transferred this issue fromdotnet/corefxon Jan 31, 2020
  9. added this to the 5.0 milestone on Jan 31, 2020
  10. added
    untriaged
    on Feb 23, 2020
  11. removed
    untriaged
    on Jul 6, 2020
  12. modified the milestones: 5.0.0, Future on Jul 6, 2020
  13. 17 remaining items

  14. KZedan commented on May 22, 2021

    @KZedan
    Contributor

    Sorry for the late reply guys, been a crazy week! 😅

    In some cases, it wouldn't make sense for me to use one of the derived types as some of them are not descriptive of usage. Shall we leave those as is or create new derived types for them?

    cc @danmoseley

  15. danmoseley commented on May 22, 2021

    @danmoseley
    Contributor

    @KZedan i suggest to make the changes where the correct type is clear. We can merge that. Then you can make an API proposal if you wish for new exception type(s) as needed.

  16. ghost added
    in-pr
    on May 23, 2021
  17. ghost removed
    in-pr
    on May 24, 2021
  18. ghost added
    in-pr
    on May 24, 2021
  19. ghost removed
    in-pr
    on May 24, 2021
  20. stephentoub commented on Sep 2, 2021

    @stephentoub
    Member

    #58529 moves Win32Exception to corelib to enable brand new APIs to throw it. I think we should reconsider that. This thread primarily looks at compatibility for existing uses, with proposals for throwing derived exceptions, in order to hide the Win32 naming in the type's ancestry rather than having it be prominent in code and logs. We should consider also introducing a new base class for Win32Exception, moving Win32Exception's logic into that base class, having new APIs that want similar behavior to throw the new exception type, and updating all of our code that catches Win32Exception to instead catch the new base.

  21. modified the milestones: Future, 7.0.0 on Sep 2, 2021
  22. modified the milestones: 7.0.0, Future on Aug 8, 2022
  23. tannergooding commented on Jul 11, 2026

    @tannergooding
    Member

    Closing. There's no way to do this in a clean and non-breaking manner for downstream users. We're largely going to always be stuck with the historical burden here.

    At best we could insert that new base class and throw some better type that derives Win32Exception, but that's really not that much better for many of these niche scenarios; particularly not in contrast to considering why the exception is needed and if it can't be provided via some other API.

  24. locked and limited conversation to collaborators on Aug 11, 2026
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

    api-suggestionarea-System.Runtimeos-linuxos-macos

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions