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

Is it possible to improve exceptions message from unbox.any? #6822

Closed
xunilrj opened this issue Oct 17, 2016 · 3 comments
Closed

Is it possible to improve exceptions message from unbox.any? #6822

xunilrj opened this issue Oct 17, 2016 · 3 comments

Comments

@xunilrj
Copy link

xunilrj commented Oct 17, 2016

I was analyzing the runtime code and realized that maybe it is possible to improve the unbox.any exception message with the involved types.
For more details see:
http://stackoverflow.com/questions/39914845/why-does-unbox-any-not-provide-a-helpful-exception-text-the-way-castclass-do

For example:

IL_0001: newobj instance void [mscorlib]System.Text.StringBuilder::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: castclass [mscorlib]System.String

As far as I understood, the runtime will step through these functions:

wich will throw the InvalidCastException

COMPlusThrow(kInvalidCastException, IDS_EE_CANNOTCAST, strCastFromName.GetUnicode(), strCastToName.GetUnicode());

This exception is detailed, it has both the castFromName and the castToName.
But with the following IL:

IL_0001: newobj instance void [mscorlib]System.Text.StringBuilder::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: unbox.any [mscorlib]System.Int64

The runtime will call this function
at
https://github.com/dotnet/coreclr/blob/32f0f9721afb584b4a14d69135bea7ddc129f755/src/vm/interpreter.cpp#L8766

for some reason the exception is thrown as

COMPlusThrow(kInvalidCastException);

So, my question is, could not the runtime do something like below and improve the exception message by having both the "From" and the "To" in its message?

auto thCastFrom = obj->GetTypeHandle();
auto thCastTo = TypeHandle(boxTypeClsHnd);
RealCOMPlusThrowInvalidCastException(thCastFrom, thCastTo);

I am still trying to compile the coreclr repository to make deeper analysis, so forget me if there is something obvious that I am no seeing.

@mikedn
Copy link
Contributor

mikedn commented Oct 17, 2016

Looks like a duplicate of #4454 which has been fixed already in .NET Core. However, the fix hasn't appeared in the .NET Framework yet.

@gkhanna79
Copy link
Member

Based upon the response above, @xunilrj is there anything outstanding this issue is tracking?

@gkhanna79
Copy link
Member

Closing this issue for now as nothing appears actionable for the moment. Please feel free to reopen if there are followup questions.

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants