-2

I'm new to coding, and very new to java, so please bear with me, I'm sorry.

My professor says we need to use the following code as part of our assignment. I've looked through all my notes for the class, and I cannot find anything on try-catch and I'm not sure what I'm supposed to put in the insert code part or what the error message means

I'm very sorry, I'm just very confused. I keep getting "Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body" and I don't know how to fix it

try 
{
    File file = new File( args [ 0 ] );
    Scanner scanner = new Scanner( file );

    //insert code

    scanner.close();
}
catch  (FileNotFoundException e)
{
 e.printStackTrace();
}

Edit: I didn't get that error again after fixing it until I tried hard-coding a text file to test things out.

try 
{
    File file = new File( args [ 0 ] );
    Scanner scanner = new Scanner("cat.txt");

    //insert code

    scanner.close();
}
catch  (FileNotFoundException e)
{
 e.printStackTrace();
}

I am once again getting "Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body"

What am I supposed to put in the try statement? I am so lost

Gabe M.
  • 1
  • 1
  • 2
    You have a missing semi-colon in your `Scanner` declaration. – Nicholas K Feb 10 '19 at 17:51
  • 1
    The error you've listed is definitely not caused by the code you've quoted. `FileNotFoundException` is indeed thrown by `new Scanner(File)`. The code you've quoted won't compile for a *different* reason (missing `;` after `new Scanner(file)`), but not because `FileNotFoundException` isn't thrown by that block. Perhaps you have a different block you haven't shown that has code that doesn't ever throw the exception...? – T.J. Crowder Feb 10 '19 at 17:51
  • Hello, I edited it, and I no longer have that error, but now when I run my code, I get " Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at templates.SentenceUtilsTest.main(SentenceUtilsTest.java:26)" – Gabe M. Feb 10 '19 at 17:56
  • you need to pass the file name or path when you are executing the code as a JVM argument. e.g. java Myclass . – Sagar Kharab Feb 10 '19 at 18:03
  • 1
    Possible duplicate of [Why is this code giving an "unreachable code" error?](https://stackoverflow.com/questions/9898549/why-is-this-code-giving-an-unreachable-code-error) – sellc Feb 10 '19 at 18:15
  • Please do not put essential information in comments to comments - stackoverflow/stackexchange is not a chat. Please do not turn the question into a different one (What causes *Unreachable catch block* → (*Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException* →) once again *Unreachable catch block*): ask a new question. – greybeard Feb 10 '19 at 22:32

2 Answers2

1

I didn't get that error again after fixing it until I tried hard-coding a text file to test things out.

try 
{
    File file = new File( args [ 0 ] );
    Scanner scanner = new Scanner("cat.txt");

    //insert code

    scanner.close();
}
catch  (FileNotFoundException e)
{
 e.printStackTrace();
}

I am once again getting "Unreachable catch block for FileNotFoundException.

new Scanner(String) is not the same as new Scanner(File). If you look at the documentation, the first (using a String) reads from the string, not from a file. Since no file is involved, there's no FileNotFoundException.

If you want to hardcode the filename for testing purposes, do that in the new File(...) line, not the new Scanner(...) line:

try 
{
    File file = new File("cat.txt");      // <==== Here
    Scanner scanner = new Scanner(file);

    //insert code

    scanner.close();
}
catch (FileNotFoundException e)
{
    e.printStackTrace();
}

That will compile, because new Scanner(File) throws FileNotFoundException.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you! I am getting a different error now, I may just post a separate question, but I think I am supposed to put more code into the try statement and I am not sure what. When I compile the code I get "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at templates.SentenceUtilsTest.main(SentenceUtilsTest.java:26)" – Gabe M. Feb 10 '19 at 18:53
0

Welcome to SO! Exceptions are a great way to catch errors and decide how to handle them. Some segments of code require the program to take a leap and attempt to perform a task that may not be possible at that moment.

The errors specified in the question are a result of the Scanner not finding the file at the specified file path. The ArrayIndexOutOfBounds is indicating exactly what the exception states. The index being accessed is outside the bounds(size) of the array.

Java docs is a great resource and this should help clarify the purpose of catch statements. https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

sellc
  • 380
  • 1
  • 5
  • 19