Newbie: try, catch problem

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

Guest

Hi

the following code should throw an exception, but it doesn't! can anyone tell me why

tr

IPAddress test = IPAddress.Parse("192.168"); //invalid inpu

catch(Exception mye

MessageBox.Show("error")
return


Any ideas

Sam Henson
 
Hi Sam!

If you just add a MessageBox.Show(test.ToString()) after you have done the
parse you will see why no exception is thrown.

When I did what you did and displayed the result this is what I got

192.0.0.168

It seems is if the pattern is like this

IPAddress.Parse("192");
Result = 0.0.0.192

IPAddress.Parse("192.168");
Result = 192.0.0.168

IPAddress.Parse("192.168.2");
Result = 192.168.0.2

And finally
IPAddress.Parse("192.168.2.1");
Result = 192.168.2.1

Why it works this way I have no clue. It would seem logical that it should
have been
192.168.0.0 instead of 192.0.0.168 but logic and real life does not always
compute :)

Good luck anyway!

//Mikael
 
...
the following code should throw an exception,
but it doesn't! can anyone tell me why?

It's simply because it is a valid input...
try
{
IPAddress test = IPAddress.Parse("192.168"); //invalid input
}
catch(Exception mye)
{
MessageBox.Show("error");
return;
}

Any ideas?

To simplify the explanation:

The dotted-quad notation allows for some "simplifications", with the *last*
quad as the significant address, everything in front of it is added to the
"network"-level, e.g.:

192 == 0.0.0.192
192.168 == 192.0.0.168
192.168.1 == 192.168.0.1

// Bjorn A
 
Back
Top