locked
Exception Handling Issue- Can continue with the code... RRS feed

  • Question

  • Hi Microsoft Team

    In my method i am using while loop and within this while loop i am using the function Conert.ToInt32("XYZWER") which throws an exception when i encounters a string and it goes to catch block.Is it possible in C# to log this exception in error log , skip "XYZWER"and continue to execute the code with next value?

    If so please let me know.Also if you can send dummy code for reference would be greta help.

    Thatnsk and Regards

    Arnab

    Thursday, August 2, 2012 1:33 PM

Answers

  • change your code to this

    while (i<=10)
    {
      try {
    
      //do something
    
      }
      catch (Exception e)
      {
      //how to make the code again go back to while loop 
      }
    }

    You can put try-catch blocks anywhere and they can be nested.

    It is more efficient to use TryParse since exception handling can be slow if there were a lot of exceptions inside the while loop.


    Dan Randolph - My Code Samples List

    • Proposed as answer by Mike Feng Friday, August 3, 2012 11:16 AM
    • Marked as answer by Mike Feng Thursday, August 16, 2012 9:24 AM
    Thursday, August 2, 2012 5:03 PM
  • u can use int.TryParse Method, which never throws an exception. MSDN Help

    int number;
          
          if (Int32.TryParse("asdadsf", out number))
          {
      //Valid number 
    }else{
    //invalid number
    }
    
    


    --Krishna

    • Proposed as answer by Mike Feng Friday, August 3, 2012 11:16 AM
    • Marked as answer by Mike Feng Thursday, August 16, 2012 9:24 AM
    Thursday, August 2, 2012 2:33 PM

All replies

  • u can use int.TryParse Method, which never throws an exception. MSDN Help

    int number;
          
          if (Int32.TryParse("asdadsf", out number))
          {
      //Valid number 
    }else{
    //invalid number
    }
    
    


    --Krishna

    • Proposed as answer by Mike Feng Friday, August 3, 2012 11:16 AM
    • Marked as answer by Mike Feng Thursday, August 16, 2012 9:24 AM
    Thursday, August 2, 2012 2:33 PM
  • Thanks Krishna , but is it possible to continue to execute the code if there is an exception thrown.

    Ex :

    try {

    While (i<=10)

    {

    //do something

    }

    }

    catch (Exception e)

    {

    //how to make the code again go back to while loop and start from the next.

    }

    because apart from Convert error my code is experiencing index out of range error.


    Arnab Ghosh Citibank Belfast (Norhern Ireland) U.K

    Thursday, August 2, 2012 2:46 PM
  • change your code to this

    while (i<=10)
    {
      try {
    
      //do something
    
      }
      catch (Exception e)
      {
      //how to make the code again go back to while loop 
      }
    }

    You can put try-catch blocks anywhere and they can be nested.

    It is more efficient to use TryParse since exception handling can be slow if there were a lot of exceptions inside the while loop.


    Dan Randolph - My Code Samples List

    • Proposed as answer by Mike Feng Friday, August 3, 2012 11:16 AM
    • Marked as answer by Mike Feng Thursday, August 16, 2012 9:24 AM
    Thursday, August 2, 2012 5:03 PM