Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip a record in a Foreach

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?

like image 426
Polo Avatar asked Jun 09 '09 12:06

Polo


1 Answers

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;
        }
    }
like image 50
Joseph Avatar answered Oct 08 '22 21:10

Joseph