-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Improved article about non-CLS exceptions #6451
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, @svick; thanks for dramatically improving this topic. I thought that the references to WrappedException would be clearer if they're fully qualified.
| - Within a `catch (Exception e)` block as a <xref:System.Runtime.CompilerServices.RuntimeWrappedException>. | ||
| - Within a `catch (RuntimeWrappedException e)` block. | ||
|
|
||
| By default, a Visual C# assembly catches non-CLS exceptions as wrapped exceptions. Use this method if you need access to the original exception, which can be accessed through the <xref:System.Runtime.CompilerServices.RuntimeWrappedException.WrappedException%2A> property. The procedure later in this topic explains how to catch exceptions in this manner. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated to your PR: add ?displayProperty=nameWithType to <xref:System.Runtime.CompilerServices.RuntimeWrappedException.WrappedException%2A>. I think that the other two occurrences of WrappedException might be clearer with ?displayProperty=nameWithType as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rpetrusha Done.
| try | ||
| { | ||
| The following example shows how to catch a non-CLS exception that was thrown from a class library written in C++/CLI. Note that in this example, the C# client code knows in advance that the exception type being thrown is a <xref:System.String?displayProperty=nameWithType>. You can cast the <xref:System.Runtime.CompilerServices.RuntimeWrappedException.WrappedException%2A> property back its original type as long as that type is accessible from your code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch for C++/CLR --> C++/CLI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making the additional changes, @svick. I'll merge your PR now.
Fixes #6446.
Changes I made:
catch (Exception ex)and then testing forRuntimeWrappedException, just usecatch (RuntimeWrappedException e). It's simpler and I don't see any reason not to do it this way.