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

Add ParamValue.ToString into ArgumentException message #14555

Closed
JesOb opened this issue May 7, 2015 · 8 comments
Closed

Add ParamValue.ToString into ArgumentException message #14555

JesOb opened this issue May 7, 2015 · 8 comments
Assignees
Milestone

Comments

@JesOb
Copy link

JesOb commented May 7, 2015

An element with the same key already exists in the dictionary.

This message says nothing when meet in log. Adding key.ToString into it will fix issue:

An element with the same key: 56 already exists in the dictionary.
An element with the same key: Bla already exists in the dictionary.
An element with the same key: WhateverReturnsFromToString already exists in the dictionary.

or

An element with the same key already exists in the dictionary. ParamValue is 56
An element with the same key already exists in the dictionary. ParamValue is Bla
An element with the same key already exists in the dictionary. ParamValue is WhateverReturnsFromToString

And so on.

@stephentoub
Copy link
Member

Dup of dotnet/corefx#1187

@JesOb
Copy link
Author

JesOb commented May 7, 2015

There is about dicitonary problem resolution but I say about ArgumentException itself for every case where we can use it.

@stephentoub
Copy link
Member

I see. ArgumentException doesn't accept nor store anything about the incorrect argument's value (other than in whatever message the caller constructs and provides), so this would be more than just an implementation change to ArgumentException: it'd actually need new API surface area, and all consumers of ArgumentException that wanted the value traced out as part of the message would need to use that new surface area. If that's what you're asking for, then it'd be helpful for you to propose the exact surface area you're interested in seeing added, at which point it can be evaluated.

@JesOb
Copy link
Author

JesOb commented May 8, 2015

Argument Exception can be overriden to have additional Ctor with ActualValue and pass value to Exception on creation of exception like in ArgumentOutOfRange exception.

And mark ctor with ParamName but without actualValue with ObsoleteAttribute to stimulate providing actualValue for Exception.

ActualValue.ToString can be surrounded with try catch and if ToString will fail we just see some info about fail and type fo Value ActualValue.GetType().FullName.

This is mostly useful in ArgumentException s in collections but actually needed everywhere.

@JesOb
Copy link
Author

JesOb commented May 8, 2015

The same in: InvalidCastException: Cannot cast from source type to destination type.
Show me source and destination type

Most of the time destination type I know but source type not.

@stephentoub
Copy link
Member

InvalidCastException is usually not explicitly thrown in code; rather, it's typically generated by the runtime when an invalid cast occurs. Such a feature request should be filed over on the https://github.com/dotnet/coreclr repo.

@terrajobst
Copy link
Member

We reviewed this item and it looks like a dupe.

@stephentoub, could you verify and close?

@stephentoub
Copy link
Member

The same in: InvalidCastException: Cannot cast from source type to destination type. Show me source and destination type

It typically does. For example, this code:

object i = new Queue<int>();
Queue<double> j = (Queue<double>)i;

results in this exception message:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.Queue`1[System.Int32]' to type 'System.Collections.Generic.Queue`1[System.Double]'.

and:

object i = "hello";
char[] arr = (char[])i;

results in the message:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Char[]'.

The source/destination types were previously left out of cast messages when unboxing, but that was recently addressed at dotnet/coreclr#1543.

Dictionary is being addressed here: dotnet/coreclr#1452 for Dictionary

And of course for your own exception throwing, you can derive whatever exceptions you like and include any additional information you desire.

With that, I don't believe there's anything else concrete and actionable on this thread, so I'm going to go ahead and close it. Thanks for opening the discussion. If subsequently there's another specific case you think needs to be addressed (i.e. somewhere an exception is being thrown where you believe it's missing important info that could/should be included), please go ahead and open another issue for that case.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 1.0.0-rtm milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 6, 2021
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

4 participants