Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to define

C#
try
{
    Label_0125:
}

catch (object obj1) 
{
    Microsoft.VisualBasic.CompilerServices.ProjectData.SetProjectError((Exception) obj1);
    goto Label_0125;
}



if i define
Exception
in
catch(Exception obj1)
then error that " No such label 'Label_0125' within the scope of the goto statement "

what i do wrong ?

please help

thanks in advance
Posted
Comments
Kim Togo 12-Aug-11 1:57am    
If you find yourself needing to use the goto statement. Reconsider.
Philippe Mori 12-Aug-11 18:52pm    
At first, it does not make much sense to loop back on an exception. And if you do need to loop, the your try/catch block should just around the code that might fail.

1 solution

Seriously!! Why goto?! You can put the label out of Try. (Ref:http://msdn.microsoft.com/en-us/library/13940fs2(v=vs.71).aspx[^])
But try to rearrange your code so that you can avoid goto.

C#
while(true) // or limit the iterations as per your wish
{
try
{
  //your task
  break;//exit loop
}
 
catch (Exception obj1) 
{
  Microsoft.VisualBasic.CompilerServices.ProjectData.SetProjectError(obj1);
}
}
 
Share this answer
 
Comments
Philippe Mori 12-Aug-11 18:54pm    
Effectively the code should look like that. Maybe with some code before the try like getting next item to process as if the failure occurs in that code, you do not want to execute again wihich might result in an infinite loop.
Prerak Patel 12-Aug-11 22:59pm    
I agree. But OP use labels and goto to try it until success. Keeping that in mind I suggested a loop like this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900