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 740871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:33:59+00:00 2026-05-14T08:33:59+00:00

I had very errorsome Exception handling with if-clauses. An exception occurs if not found

  • 0

I had very errorsome Exception handling with if-clauses. An exception occurs if not found path. An exception returns the function again. The function is recursive. Safe?

$ javac SubDirs.java 
$ java SubDirs
Give me an path.
.
Dir1
Dir2
Give me an path.
IT WON'T FIND ME, SO IT RETURNS ITSELF due to Exception caught
Give me an path.

The code of Subdirs.java looks like this:

import java.io.*;
import java.util.*;

public class SubDirs {
    private List<File> getSubdirs(File file) throws IOException {
        List<File> subdirs = Arrays.asList(file.listFiles(new FileFilter() {
                    public boolean accept(File f) {
                    return f.isDirectory();
                    }
                    }));
        subdirs = new ArrayList<File>(subdirs);

        List<File> deepSubdirs = new ArrayList<File>();
        for(File subdir : subdirs) {
            deepSubdirs.addAll(getSubdirs(subdir)); 
        }
        subdirs.addAll(deepSubdirs);
        return subdirs;
    }

    public static void search() {
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String s;
            System.out.println("Give me an path.");
            while ((s = in.readLine()) != null && s.length() != 0){
                SubDirs dirs = new SubDirs();
                List<File> subDirs = dirs.getSubdirs(new File(s));
                for ( File f : subDirs ) {
                    System.out.println(f.getName());
                }
                System.out.println("Give me an path.");
            }
        }catch(Exception e){
                    // Simple but is it safe?
            search();
        }
    }

    public static void main(String[] args) throws IOException {
        search();
    }
}
  • 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-14T08:34:00+00:00Added an answer on May 14, 2026 at 8:34 am

    Debatable. I would think this leaves a mess of useless stack frames – maybe not a concern unless you try to reuse this code on a mobile device. Regardless, I wouldn’t let it pass a code review without more convincing that there’s no better way.

    For better understandability and maintainability, get in the habit of coding what you mean: if you intend the code to loop to retry on error, then code a loop. Recursion feels slicker, but, since its not a common practice in exception handlers, it increases the surprise factor. if I were a future maintainer of this code, I’d wonder if the recursion is intentional or a bug – please leave me a comment to explain the recursion is intentional.

    I would gladly exchange a few more lines of mundane code now for better maintainability and easier troubleshooting later.

    public static void search() {
    
      boolean succeeded = false;
      int attemptsLeft = MAX_ATTEMPTS;
      while (! succeeded && (--attemptsLeft > 0)) {
    
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String s;
            System.out.println("Give me an path.");
            while ((s = in.readLine()) != null && s.length() != 0){
                SubDirs dirs = new SubDirs();
                List<File> subDirs = dirs.getSubdirs(new File(s));
                for ( File f : subDirs ) {
                    System.out.println(f.getName());
                }
                System.out.println("Give me an path.");
            }
    
            succeeded = true;
    
        }catch(Exception e){
            // ALWAYS LEAVE YOURSELF A CLUE TO WHAT HAPPENED
            // e.g. the exception caught may not be the exception you expected
            e.printStackTrace();
        }
    
      } // while not succeeded yet
    
      if (! succeeded) {
        log("Hmm ... bailing search() without succeeding");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a very difficult time with understanding the root cause of a problem
We had a very interesting problem with a Win Forms project. It's been resolved.
Ok, I asked about this very error earlier this week and had some very
We're starting to standardise on a Ruby-based testing framework, having had some very good
So I had to create this splash page in a very short amount of
This is very odd. I have never had this issue with ShowDialog in C#.
I'm very much a vb person, but have had to use this id number
I find jsfiddle very useful for client side web development. I have had a
ACL seems to be very confusing and never problem-free. Although, I've had no major
Ok, I'm now very confused. After my last question had several people comment about

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.