Continue <label> in C # as in java

Do I need the C # Javas equivalent to continue?

I have

for (String b : bar) { <label> try { } catch (EndpointNotFoundException ex) { continue <label> } } 

how can i simulate this in c #. I need so that when I get an exception that I am repeating code, do not continue.

+4
source share
4 answers

Use goto <label>;

+5
source

If you only need to continue the next iteration of the loop, use continue .

C # also has labels (which you can jump with goto ), but don't use them.

+5
source

Why not just add a control variable?

 foreach (String b in bar) { bool retry; do { retry = false; try { // ... } catch (EndpointNotFoundException ex) { retry = true; } } while (retry); } 

or

 var strings = bar.GetEnumerator<string>(); var retry = false; while (retry || strings.Next()) { var b = strings.Current; retry = false; try { // ... } catch (EndpointNotFoundException ex) { retry = true; } } 
+5
source

I donโ€™t think that what you are trying to do is reasonable at all - do you have any reason to expect that you will not run into a scenario where you always get an exception at a certain iteration?

Anyway, to do what you want to do without goto , look at this:

 foreach (String b in bar) { while(!DidSomethingWithThisString(b)) ; } bool DidSomethingWithThisString(string b) { try { } catch (EndpointNotFoundException ex) { return false; } return true; } 
+2
source

Source: https://habr.com/ru/post/1304608/


All Articles