Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

File Not Found Exception How to catch

807597Mar 6 2005 — edited Mar 6 2005
What's wrong with my use of the catch statement below in attempting to catch a file not found exception? My IDE says "Unreachable catch block for FileNotFoundException this exception is never thrown from the try statement body, how do I correct this? It also does not like the (temp) in the return statement says local variable temp may not have been initialized.
	public static Scanner openInputFile() throws FileNotFoundException
	{
	    String temp;
	    Scanner in = new Scanner(System.in);
	try
	{

	System.out.println("Please enter the name of the input file.");
	temp = (in.nextLine());
	return new Scanner(new File(temp));
	}
	catch(FileNotFoundException e)
	{
	    System.out.println("FileNotFoundException: " + e.getMessage());
	}


	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 3 2005
Added on Mar 6 2005
7 comments
980 views