You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Reacted by Ben Gribaudo, AraHaan and Rodion Mostovoi
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.
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.
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.
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.
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?
@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.
ghost added
in-prThere is an active PR which will close this issue when it is merged
#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.
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.
Activity
stephentoub commentedon May 20, 2018
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.
tdinucci commentedon May 20, 2018
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.
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.
danmoseley commentedon May 20, 2018
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.
jnm2 commentedon May 28, 2018
Plus,
HResultis defined on the baseExceptionclass.asyncawaitmvvm commentedon Jul 6, 2018
Oh you guys.
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.
danmoseley commentedon Jul 7, 2018
@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?
bgribaudo commentedon Aug 15, 2019
A similar idea occurred to me when working on adding dotnet/corefx#34147. Would be nice to see something like this. :-)
17 remaining items
KZedan commentedon May 22, 2021
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
danmoseley commentedon May 22, 2021
@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.
stephentoub commentedon Sep 2, 2021
#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.
tannergooding commentedon Jul 11, 2026
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.