Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3975946
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:44:46+00:00 2026-05-20T04:44:46+00:00

I have the following method I’m working on: private IEnumerable<TreeNode> GetChildNodes(TreeNode parent) { string

  • 0

I have the following method I’m working on:

    private IEnumerable<TreeNode> GetChildNodes(TreeNode parent)
    {
        string path = parent.Tag.ToString();

        // Add Directories
        string[] subdirs = Directory.GetDirectories(path);

        foreach (string subdir in subdirs)
        {
            yield return GetChildNode(subdir);
        }

        // Add Files
        string[] files = Directory.GetFiles(path);

        foreach (string file in files)
        {
            var child = GetChildNode(file);
            fileNodeMap[file] = child;
            yield return child;
        }
    }

This works fine with the exception of Directory.GetDirectories() and Directory.GetFiles() can both throw exceptions that I want to catch.

I can’t catch the pieces of code which utilize those methods due to my use of yield (yields can’t be placed within the body of a try if there is a catch). I know I could remove the yield and simply add to my children to a collection but I’m curious how someone would catch IOExceptions from both of those methods and still utilize yield?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:44:47+00:00Added an answer on May 20, 2026 at 4:44 am

    How about something like (for the first part):

    string[] subdirs;
    try
    {
        subdirs = Directory.GetDirectories(path);
    }
    catch (IOException e)
    {
        // Do whatever you need here
        subdirs = new string[0];
    }
    

    And similarly for the second. You don’t need to yield within that try block. If this doesn’t help, please write whatever code you would want to be valid, so that we can see what you’re planning to do if an exception is thrown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following serialization method: Private Function SerializeData(ByVal data As cData) As String
I have the following method in some nemerle code: private static getLinks(text : string)
I have a following method private void SetProcessDocumentStatus(string status) { var setStatusWith = new
I have the following method in my code: private bool GenerateZipFile(List<FileInfo> filesToArchive, DateTime archiveDate)
Say we have the following method: private MyObject foo = new MyObject(); // and
I have the following class: public abstract class AbstractParent { static String method() {
I have the following method GetData that creates a StreamReader from a file. private
I have following example method: namespace Postcode_webservice { public class Business { public string
I have the following method: private DataTable getsortedtable(DataTable dt) { dt.DefaultView.Sort = Name desc;
I have the following method within my TcpServer private void Receive(ZClient client) { NetworkStream

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.