none
Continue a foreach loop after exception

    Question

  • Hey friends.

    How to continue to iterate in a foreach loop after a exception is throw ?

    For example:




    ManagementClass mcWep = new ManagementClass("root\\WMI","MSNdis_80211_AddWep", null);

    ManagementObjectCollection mocWep = mcWep.GetInstances();


    try
    {
       foreach (ManagementObject moWep in mocWep)    <--- This line may throw excetion on some itens
       {
          // some code
       }
    catch(Exception ex)
    {
       // more code
    }


     



    I want to continue to iterate throug the collection ignoring the item that throws the exception. I tried to isue a continue on catch block, but its out of scope. Tried goto but, again, problems with escope.

    I can´t interate using convencional for loop since i don´t have index in "mocWep".

    Any help ?

    Thank you.


    Alexandre Parreira

    Thursday, October 13, 2005 4:33 PM

Answers

  • You should loop and get Object and then cast into ManagementObject


    ManagementClass mcWep = new ManagementClass("root\\WMI","MSNdis_80211_AddWep", null);


    ManagementObjectCollection mocWep = mcWep.GetInstances();
    ManagementObject moWep;

    foreach (Object o in mocWep)
    {
     try
     {
      moWep = (ManagementObject)o; // Exception will be here
     }
     catch(Exception e)
     {

     }
    }


    or, with no exception

    {
     if(typeof(ManagementObject) == o.GetType())
     {
      moWep = (ManagementObject)o;
     }
    }

    Thursday, October 13, 2005 5:08 PM
  • If this still does not work, you will have to loop through the index instead of the object

    for(int index = 0; index < mocWep.Count; index++)
    {
     moWep = (ManagementObject)mocWep[ index ];

    }

    Friday, October 14, 2005 12:29 PM

All replies

  • You should loop and get Object and then cast into ManagementObject


    ManagementClass mcWep = new ManagementClass("root\\WMI","MSNdis_80211_AddWep", null);


    ManagementObjectCollection mocWep = mcWep.GetInstances();
    ManagementObject moWep;

    foreach (Object o in mocWep)
    {
     try
     {
      moWep = (ManagementObject)o; // Exception will be here
     }
     catch(Exception e)
     {

     }
    }


    or, with no exception

    {
     if(typeof(ManagementObject) == o.GetType())
     {
      moWep = (ManagementObject)o;
     }
    }

    Thursday, October 13, 2005 5:08 PM
  • Thank you for your help.

    This solves casting exceptions but my problem is that i have a "not supported" exception ("System.Management.ManagementException").
    On this case im trying to advance to next object.

    Still looking for a way to "continue" on exception in a foreach loop.

    Thank you.

    ALexandre

    Friday, October 14, 2005 12:08 PM
  • If this still does not work, you will have to loop through the index instead of the object

    for(int index = 0; index < mocWep.Count; index++)
    {
     moWep = (ManagementObject)mocWep[ index ];

    }

    Friday, October 14, 2005 12:29 PM