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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:16:02+00:00 2026-06-15T23:16:02+00:00

Inside a method, I use a Scanner to read text inside a file. This

  • 0

Inside a method, I use a Scanner to read text inside a file. This file doesn’t always exist, and if it doesn’t, I want simply to do nothing (i.e. no scan).
Of course I could use a try/catch like this:

String data = null;
try
{
    Scanner scan = new Scanner(new File(folder + "file.txt"));
    data=scan.nextLine();
    scan.close();
}
catch (FileNotFoundException ex)
{
}

My question is what can I do to avoid the try/catch? Because I don’t like local variable unused. I was thinking of something like:

String data = null;
File file_txt = new File(folder + "file.txt");
if (file_txt.exists())
{
    Scanner scan = new Scanner(file_txt);
    data=scan.nextLine();
    scan.close();
}

But of course with this I get an error in Netbeans and I can’t build my project…

  • 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-15T23:16:03+00:00Added an answer on June 15, 2026 at 11:16 pm

    No, It’s checked exception. try must be followed with either catch block and/or finally block. There are two method for handling checked exception.

    Method 1 : Either wrap your code using try/catch/finally

    Option 1

    try{
        Scanner scan = new Scanner(new File(folder + "file.txt"));
        data=scan.nextLine();
        scan.close();
    
    }
    catch (FileNotFoundException ex)
    {
       System.out.println("Caught " + ex);
    }
    

    Option 2

    try{
        Scanner scan = new Scanner(new File(folder + "file.txt"));
        data=scan.nextLine();
        scan.close();
    
    }
    finally
    { 
          System.out.println("Finally ");
    }
    

    Option 3

        try{ 
         Scanner scan = new Scanner(new File(folder + "file.txt"));
         data=scan.nextLine();
         scan.close();
         }catch(FileNotFoundException ex){
              System.out.println("Caught " + ex );
         }finally{
              System.out.println("Finally ");
         }  
    

    Method 2: Throw exception using throw and list all the exception with throws clause.

        class ThrowsDemo {
    
        static void throwOne() throws IllegalAccessException {
            System.out.println("Inside throwOne.");
            throw new IllegalAccessException("demo");
        }
    
        public static void main(String args[]) {
            try {
                throwOne();
            } catch (IllegalAccessException e) {
                System.out.println("Caught " + e);
            }
        }
        }
    

    Note : Checked Exception means Compiler force you to write something to handle this error/exception. So, AFAIK, there is no any alternative for checked exception handling except above method.

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

Sidebar

Related Questions

I'm trying to use depedency resolver inside a Web Api method. This worked fine
I want to emit a method that returns a Func<>. Inside this method I
I've always thought that it's impossible for this to be null inside instance method
Can i use @value annotation inside a method to read property? void method1(){ @Value(#{AppProperties['service.name']})
When using an anonymous innerclass inside a method, when we want to use a
I need to Use folowing code inside the method onProviderDisabled. But it doesn't last
I need to use a class callback method on an array inside another method
I want to call a validation method inside a shared gwt class that i
is it possible to use getitem inside a method, ie Class MyClass: @property def
Is it possible to use the getResources() method inside the Runnable() ? . When

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.