I want to do the following job
try
{
if(......)
Exit Try //this will do in VB but not in C#
//do stuff
if(............)
Exit Try
//do stuff
}
catch()
{
}
I have tried using do{ }while(false); and using Switch - Case.
Is there any other way to do this.
Reply soon
Loading
TomPosted Apr 19, 2017, 10:42 AM
Ex.
AlanPosted Jul 31, 2007, 12:32 PM
I agree with Mike that we don't really need this construct in C#, as you can always code around it. If you find yourself in a deeply nested loop within the try block, without any further possibility of an exception being thrown, then you can always use a 'goto label' which is all that Exit Try really is under the hood.
Another adornment which VB's Try statement has but C# lacks is the When filter on the Catch block. Again I've never been able to see much point of this as you can always simulate it by placing an If clause in the Catch block. In fact the only time I've seen it used in VB is where you're catching exceptions based on VB's old error number system.
Mike GoldPosted Jul 31, 2007, 2:07 AM
Hi,
. Hey, but that is just me.
I do not believe there is an equivalent to exit try in C# (other than goto label). The reason there is no equivalent is a good one. Don't do it! It's horrible coding practice. It would even be better to throw an exception (instead of exit try) and catch it somewhere and handle the rest in a finally block.
Why is it in VB then? Because early VB programmer's(before .NET) are probably used to having the option of less tidy programming structures, but personally I think it should be removed completely from VB.NET which is supposed to encourage good programming practices
Jan MontanoPosted Jul 31, 2007, 2:04 AM
Why would you want to exit your try clause? The try clause is there to catch exceptions. What exactly do you want to do? If you just want to control the flow of your program, I think there's a way with if conditions or switch statements. If you could post your code, maybe we could help.
Regards,
Jan