try-catch, go to catch by statement

  • Thread starter Thread starter macneed
  • Start date Start date
M

macneed

try
{
if(i==3)
goto catch;

Console.WriteLine(i);

}
catch
{
Console.WriteLine("i = 3");
}


any statement can do the below job?
goto catch;
or i must write a dirty statement like open a non exceed file?

Thanks
 
macneed was thinking very hard :
try
{
if(i==3)
goto catch;

Console.WriteLine(i);

}
catch
{
Console.WriteLine("i = 3");
}


any statement can do the below job?
goto catch;
or i must write a dirty statement like open a non exceed file?

Thanks

you could throw an exception yourself:
throw new Exception("i == 3");

There is no need to do something illegal just to get an exception to be
thrown.
But I don't think you should use exceptions for this. Why not just a
regular if/else?

Hans Kesting
 
            try
            {
                if(i==3)
                    goto catch;

                Console.WriteLine(i);

            }
            catch
            {
                Console.WriteLine("i = 3");
            }

any statement can do the below job?
goto catch;
or i must write a dirty statement like open a non exceed file?

Check out "throw"
 
            try
            {
                if(i==3)
                    goto catch;

                Console.WriteLine(i);

            }
            catch
            {
                Console.WriteLine("i = 3");
            }

any statement can do the below job?

bool failed = false;
try
{
if (i == 3)
{
failed = true;
}
else
{
Console.WriteLine(i);
}
}
catch
{
failed = true;
}
if (failed)
{
Console.WriteLine("i = 3");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top