10

I am trying to create an object from an Active Directory base on a simple login. The problem is that some of the login information is valid.

How could I just use a try-catch so that if an exception is thrown, just skip to the next login?

Here is the code:

foreach (var PharosUserItem in ListRef) { ADUser User; try { User = new ADUser(PharosUserItem.UserLoginPharos); } catch (ByTel.DirectoryServices.Exceptions.UserNotFoundException ex) { break; } } 

The break gets me out of the foreach, which is not what I want. Any ideas?

3
  • I guess an empty catch will do the job for you... Commented Jun 9, 2009 at 12:33
  • 1
    Nope continue is the best way, beacause there are actions after that i have not shown. Commented Jun 9, 2009 at 12:47
  • Personnaly I'll use the User variable inside the try block and simply catch the exception with no break nor continue. Commented Jun 9, 2009 at 13:03

6 Answers 6

34

You are looking for continue

foreach (var PharosUserItem in ListRef) { ADUser User; try { User = new ADUser(PharosUserItem.UserLoginPharos); } catch (ByTel.DirectoryServices.Exceptions.UserNotFoundException ex) { continue; } } 
Sign up to request clarification or add additional context in comments.

Comments

13

Use the continue statement instead:

foreach (var pharosUserItem in ListRef) { ADUser user; try { user = new ADUser(pharosUserItem.UserLoginPharos); } catch (UserNotFoundException) { continue; } // Use "user" here } 

(I've made a few changes in terms of variable casing, avoiding using a massively long fully-qualified name for the exception, and providing a variable for the exception which you then ignore.)

Note that if there's any reasonable way that you could get a list of valid users and check it, that would be nicer than using the exception for flow control as you're doing here. It may not be feasible, but it's worth checking :)

2 Comments

Does the 'continue' statement have to be the last logical statement in the code or can it be higher up? I am assuming it will literally take the execution to the end of the foreach statement and go back up again?
@Fandango68: When faced with a question like that, I'd start off by experimenting, and then reading the relevant language specification. (But no, it doesn't have to be the last statement in the code.)
5

You have to use continue;

Comments

5

Use the continue statement to skip to the next iteration.

Comments

1

Rather than throwing an exception, instead you should try and check if the user is valid first. Throwing exceptions is quite expensive and should really only be used in 'exceptional' circumstances and not to control the logical flow of the app, this isn't what they should be used for as you are expecting some users to fail.

Comments

0

why not use nothing instead of continue?

Use the continue statement instead:

foreach (var pharosUserItem in ListRef) { ADUser user; try { user = new ADUser(pharosUserItem.UserLoginPharos); } catch (UserNotFoundException) { } // Use "user" here } 

or put a

 console.writeline("user not found: "+ pharosuseritem.tostring() ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.