pls give me the solution

Reply Forum
Forum postsuganthi @ 2011-03-03 10:06:17
ReplyReply to
i wrote the following simple code... But if i give any value more than 10 its not entering into catch block... i dunno why this problem in the code.. pls someone clear my doubt....
using System;
using System.Collections;


namespace ifstatement
{
class Program
{
static void Main(string[] args)
{
int i;




i = 10;
for (i = 0; i < 10; i++)

{
Console.WriteLine("pls enter the number");
int number = int.Parse(Console.ReadLine());

try
{
Console.WriteLine(i);
if (number == 0 && number <= 0)
Console.WriteLine("enter the valid number");

else
if (number > 1 && number < 10)
{
Console.WriteLine("exceeds ur birth date");
}

}
catch(Exception ex)
{
Console.WriteLine("i catched you"+ex.Message);
}

finally
{
Console.WriteLine("out of catch");
}
}





}
}
}


Forum postMiezan @ 2011-03-12 01:18:37
ReplyReply to
hey i hope it is not to late :)
here is what i did with comment to explain the changes:
enjoy

/* first a little explaining of the program will help :)
like what do u really expect to have as result...
i modified ur code so as to make sense to me , mainly by switching around the try{} and for{} blocks
it look kinda weird to see the finally block being repeated at each iteration
*/
namespace ifstatement
{
class Program
{
static void Main(string[] args)
{

int i;
// i = 10; // not need at all ur not using it
try
{
for (i = 0; i < 10; i++)
{
Console.WriteLine("pls enter the number");
int number = int.Parse(Console.ReadLine());

Console.WriteLine(i);

if (number <= 0) // <= 0 mean less or equal to 0 so ==0 is not needed
Console.WriteLine("enter the valid number");

else if (number >= 1 && number < 10) // i believe 1 should be included also
{
Console.WriteLine("exceeds ur birth date");
}
else //you need a default case
{

throw new Exception() ; // this is the line you need, it throw an exception if the default else is reached
}

}
}
catch (Exception ex)
{
Console.WriteLine("i catched you " + ex.GetType().ToString()); // space after "...you" :)
}

finally
{
Console.WriteLine("out of catch");
}
Console.ReadKey();
}
}
}


Forum postRyan @ 2011-03-25 07:18:22
ReplyReply to
Hi there,

you can get all ur queries solved @ www.projectsdeal.com

A very good platform to showcase ur project and to get any project. happy tech surfing! :)


Forum postRyan @ 2011-03-25 07:30:52
ReplyReply to
Hi there,

you can get all ur queries solved @ www.projectsdeal.com

A very good platform to showcase ur project and to get any project. happy tech surfing! :)


ps: Dont hestitate to call them. i did and it helped me incredibly!


Forum postsurjeet @ 2011-05-10 08:41:00
ReplyReply to
try
{
Console.WriteLine(i);
if (number == 0 && number <= 0)
Console.WriteLine("enter the valid number");

else
if (number > 1 && number < 10)
{
Console.WriteLine("exceeds ur birth date");
}

}
catch(Exception ex)
{
Console.WriteLine("i catched you"+ex.Message);
}

Hi,
actually catch block is corresponding to a try block ,and if any error will come in try block then only catch block will execute but here in yr try block after first line u have written two conditional statement
if (number == 0 && number <= 0)

else
if (number > 1 && number < 10)

this means if number is either <=0 or between 1-10 then only yr try will fully execute ,in case of number >10 it will not execute in yr conditions so if error comes yr catch block will not execute.

for that u should write like this

try
{
Console.WriteLine(i);
if (number == 0 && number <= 0)
Console.WriteLine("enter the valid number");

else
if (number > 1 && number < 10)
{
Console.WriteLine("exceeds ur birth date");
}

else

{
Console.WriteLine("exceeds ur birth date");
}

}
catch(Exception ex)
{
Console.WriteLine("i catched you"+ex.Message);
}











© net-tutorials.com 2006 - 2012

MailContact net-tutorials.com