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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:46:08+00:00 2026-06-09T03:46:08+00:00

I have just learned how to use exceptions, and I have a bit of

  • 0

I have just learned how to use exceptions, and I have a bit of confusion with RuntimeExceptions since these specifically are unchecked. So I have this code:

    String path = "foo/bar/local/stuff.txt";
    File file = new File(path);

Isn’t this not a FileNotFoundException if that path isn’t found? How do I specify that in my method? Also after this step I do:

    File[] listFiles = file.listFiles();

Doesn’t that also throw an exception if:
1. It’s not a directory, it’s a file.
2. It doesn’t exist.

Sorry if this seems like newbie questions, I still don’t really know how to use exceptions. Can someone please clarify?

  • 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-06-09T03:46:10+00:00Added an answer on June 9, 2026 at 3:46 am

    Alright, I’m adding code, so I’m just gonna add a new answer. The listFiles method will return null if the file is not a directory. To throw your own exception, do this:

    public void exampleMethod() throws Exception {
        File[] files = someFile.listFiles();
        if(files == null) {
            throw new Exception("File is not a directory, and as such cannot list files");
            //You can also create your own exception class (extends Exception), and explicitly throw that, rather than just a general Exception
            return;
        }
    
    }
    

    If you just want it to print the stuff, and not throw an exception, just do

    new Exception("blah").printStackTrace();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just learned how to use the SQLite database for local storage in
I have just learned about the possibility to use OTHER_CODE_SIGN_FLAGS to specify the keychain
I have learned to use the SCOPE_IDENTITY() instead of just @@IDENTITY to get the
I use this syntax to open my files, since I learned that some years
I am new to Box2d, and I have just learned that it does not
I just learned (the hard way) that Java Component s can only have one
I have just created this table: create table dbo.OrderTypes ( ID smallint primary key
We have just migrated from solr3.5 to solr3.6, for all this time we have
im fairly new to databases in vb.net and i have just learned how to
I just learned ruby, and already have some html/javascript/css background. When i learn rails

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.