Answered by:
Exception is thrown, but for loop doesn't continue.

Question
-
Hello, I am trying to get all the files in the program files directory, but some of the files are being used by another process and when I come across a file that is being used, the System.IO.IOException is thrown, but I have a try - catch block and catch the error. After I enter error handling mode and complete error handling mode, the program just exits the void. It doesn't continue to the next file in the for loop. Any Help? Thanks.
-John
Friday, July 29, 2011 6:37 PM
Answers
-
try { for( int i=0; i<10; i++ ) { Console.WriteLine( i ); throw new Exception( "boom!" ); } } catch( Exception x ) { Console.WriteLine( x ); // execution does not return into the try block }
This outputs 0 then the exception message. it doesn't keep going 1 exception 2 exception 3 exception, etc..There's no "resume" for exceptions.
If you wanted to catch and continue the loop, then the try must be inside the loop.
for( int i=0; i<10; i++ ) { try { Console.WriteLine( i ); throw new Exception( "boom!" ); } catch( Exception x ) { Console.WriteLine( x ); // execution still does not return into the try block, but // the loop continues and a new try block is entered on the // next pass. } }
This outputs 0 exception 1 exception 2 exception, etc...- Marked as answer by Lie YouModerator Monday, August 1, 2011 8:44 AM
Friday, July 29, 2011 7:23 PM -
Outside
That's why. Put it inside the loop.
Ctrl+Z- Marked as answer by Lie YouModerator Monday, August 1, 2011 8:44 AM
Friday, July 29, 2011 7:30 PM
All replies
-
There are several possibilities; the try catch might be in the wrong place, another exception might be thrown in the catch block, you might be breaking the file enumerator, just to name a few. Without looking at the code in question it's really hard to say.
Thanks
--mcFriday, July 29, 2011 7:07 PM -
Hello,
Is the try/catch inside the loop or outside of the loop? It makes a difference.
Adam
Ctrl+Z- Proposed as answer by Rudedog2 Saturday, July 30, 2011 1:50 PM
Friday, July 29, 2011 7:19 PM -
try
{
String[] files = System.IO.Directory.GetFiles(Enviroment.GetFolderPath(Enviroment.SpecialFolder.ProgramFiles), "*.*", SearchOption.AllDirectories);
/* Code Removed */
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Friday, July 29, 2011 7:21 PM -
try { for( int i=0; i<10; i++ ) { Console.WriteLine( i ); throw new Exception( "boom!" ); } } catch( Exception x ) { Console.WriteLine( x ); // execution does not return into the try block }
This outputs 0 then the exception message. it doesn't keep going 1 exception 2 exception 3 exception, etc..There's no "resume" for exceptions.
If you wanted to catch and continue the loop, then the try must be inside the loop.
for( int i=0; i<10; i++ ) { try { Console.WriteLine( i ); throw new Exception( "boom!" ); } catch( Exception x ) { Console.WriteLine( x ); // execution still does not return into the try block, but // the loop continues and a new try block is entered on the // next pass. } }
This outputs 0 exception 1 exception 2 exception, etc...- Marked as answer by Lie YouModerator Monday, August 1, 2011 8:44 AM
Friday, July 29, 2011 7:23 PM -
OutsideFriday, July 29, 2011 7:24 PM
-
Outside
That's why. Put it inside the loop.
Ctrl+Z- Marked as answer by Lie YouModerator Monday, August 1, 2011 8:44 AM
Friday, July 29, 2011 7:30 PM -
Debugging the program right now. Thanks!
Friday, July 29, 2011 7:32 PM