all groups > dotnet clr > july 2005 >
You're in the

dotnet clr

group:

InvalidCastException


InvalidCastException cody
7/19/2005 12:00:00 AM
dotnet clr:
why doesn't an invalidcastexception contain the value which I was attempting
to cast and the type I was attempting to cast it to? Shouldn't the CLR know
these values?
Iam just trying to understand was is going on under the hood when such an
exception is thrown.

Re: InvalidCastException Scott M.
7/19/2005 7:51:41 PM
If you look at the exception's stacktrace, you will see what you are looking
for.


[quoted text, click to view]

Re: InvalidCastException Matthijs van der Vleuten
7/20/2005 12:00:00 AM
[quoted text, click to view]

No, the stack trace is very useful. Just look at the first frame, the
Re: InvalidCastException cody
7/20/2005 12:00:00 AM
Sorry that I was unclear Iam not talking about debugging but about catching
the exception and doing stuff depending on the values in the exception.


"Matthijs van der Vleuten" <microsoft-nntp@chroma.mine.nu> schrieb im
Newsbeitrag news:#4aE99SjFHA.3692@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Re: InvalidCastException Mrinal Kamboj
7/20/2005 12:00:00 AM
Way it goes is :

InvalidCastException is thrown only when tried between types which are
possible to typecast for e.g:

string s = "1" ;
int i = (int) s ; // Compiler Error

Object s = "1" ;
int i = (int) s ; // Invalid Cast Exception

so even if you do something like :

int j = 1 ;
object s = j.toString() ;
int i = (int) s ; // Invalid Cast Exception

so , invariably at runtime what is inside an object type is unknown ,
till the point it applies the typecasting logic and try to map it to
given data type , when it leads to exception .

I think this makes it clear , that why info is limited in exception
block , but i think if u try deriving a new class from it , then you may
be able to get something that u need .

Mrinal


[quoted text, click to view]
Re: InvalidCastException cody
7/20/2005 10:55:03 AM
Iam not interested in the stacktrace but in the value which was tried to
cast, and the type which it was tryied to cast to. It would be great if the
InvalidCastException would have these properties.


"Scott M." <s-mar@nospam.nospam> schrieb im Newsbeitrag
news:u42YzzLjFHA.1412@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Re: InvalidCastException Jon Skeet [C# MVP]
7/20/2005 5:35:40 PM
[quoted text, click to view]

Yes, but I believe what the OP is trying to say is that the exception
could say what the actual type was, like it does in Java. Consider the
following two programs:

Java:
public class Test
{
public static void main (String[] args)
{
try
{
Object o = new Object();
String s = (String)o;
}
catch (Exception e)
{
System.out.println(e);
}
}
}

C#:
using System;

public class Test
{
public static void Main (String[] args)
{
try
{
Object o = new Object();
String s = (String)o;
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}


Results (with stack traces removed):

Java:
java.lang.ClassCastException: java.lang.Object

C# (with .NET 1.1):
System.InvalidCastException: Specified cast is not valid.

C# (with .NET 2.0 beta):
System.InvalidCastException: Unable to cast object of type
'System.Object' to type 'System.String'.


So it seems that .NET 1.1 is the worst offender, giving you no
information at all about the types involved. Java is somewhat better
telling you what you were trying to cast, but not what you were trying
to cast it to. .NET 2.0 is the best of the bunch, telling you both
types involved.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Re: InvalidCastException cody
7/21/2005 9:30:42 AM
[quoted text, click to view]

that's great :)

AddThis Social Bookmark Button