try-catch, go to catch by statement

try-catch, go to catch by statement

Post by macnee » Wed, 27 Aug 2008 19:02:56



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
 
 
 

try-catch, go to catch by statement

Post by Peter Morr » Wed, 27 Aug 2008 20:04:46

throw an exception :-)

 
 
 

try-catch, go to catch by statement

Post by Hans Kesti » Wed, 27 Aug 2008 20:06:12

macneed was thinking very hard :

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-catch, go to catch by statement

Post by Michaela M » Wed, 27 Aug 2008 20:08:05


Check out "throw"
 
 
 

try-catch, go to catch by statement

Post by sloa » Wed, 27 Aug 2008 20:14:41

Oh man. Please don't think in "goto" terms, let alone try to type them.


......

http://www.yqcomputer.com/
(A great post by Krzysztof Cwalina on exceptional handling)
 
 
 

try-catch, go to catch by statement

Post by Pavel Mina » Wed, 27 Aug 2008 23:10:38


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