CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2004
    Posts
    57

    I'm getting a FileNotFoundException error...

    I'm using scanner to read from a txt file, and I have the following:

    Code:
    Scanner scan = new Scanner(new File("grades.txt"));
    and I'm getting this error:

    unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown.

    I also tried putting grades.txt at c:/grades.txt and did new File("c:/grades.txt") but that still gave me the same error. Any ideas?

  2. #2
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: I'm getting a FileNotFoundException error...

    That just means you have to handle an exception, put that code in a try/catch block:
    Code:
     
    try 
    {   Scanner scan = new Scanner(new File("grades.txt"));
    .........
    } 
    catch (FileNotFoundException e) { e.printStackTrace(); }
    or the alternative is to declare the method to throw an exception where that code is in:
    Code:
    void read() throws FileNotFoundException
    {     Scanner scan = new Scanner(new File("grades.txt"));
    .....
    }
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  3. #3
    Join Date
    Sep 2004
    Posts
    57

    Re: I'm getting a FileNotFoundException error...

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured