try to generate System.InvalidCastException intentionally

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to intentionally cast a string value into an integer, such as the
text "Jack" into a Integer variable to generate the exception
InvalidCastException. This is for an excercize on generating exceptions.
However, the compiler is catching the would be mistake before i can run the
program in the first place so I am stuck.

Any ideas please.
 
Hello,

Instead of declaring the text as a string, but as an object, then try to cast it.

object s = "Jack";
int i = (int)s;

Kelly
 
Back
Top