Not Permitted to Use File Not Found 异常,但是有必要
Not Permitted to Use File Not Found Exception, but it is necessary
所以我在尝试使用 try-catch 时遇到了一个奇怪的问题,但它不允许出现异常。我会尽量清楚地描述问题。
一些背景:
好的,任务基本上是允许用户写入和读取一些文件。所以我在我的代码中调用一个以前制作的文件的部分。用户可以输入他们希望调用的文件,因此这个例外当然是强制性的。但是错误说永远不会抛出异常,但是当我 运行 代码时它确实被抛出。让我给你看一些代码。
`while(error){
try{
response = scan.nextLine();
error = false;
}catch(FileNotFoundException e){
System.out.println("\nERROR: ");
error = true;
}
}`
好的,这是我编写的代码。现在这是我的输出(运行ning 没有 try-catch)
This is my output without using a try-catch
这是我的错误:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem: Unreachable catch block for FileNotFoundException. This
exception is never thrown from the try statement body
at fileLab2.MemoMaker.main(MemoMaker.java:126)
希望有人能帮助解决这个问题。我就是为了这个问题才开这个账号的,老实说,挺烦人的。
请询问您是否需要任何说明。
这是我的整个程序:
/*
* tslaurenx
* File reader lab 2
* OOP
* 11/20/15
* Lab 8b
*
*/
package fileLab2;
import java.util.*
import java.io.*;
public class MemoMaker {
public static void main(String[] args) throws IOException {
int choice = 0;
String name = "", response = "", memo = "";
boolean running = true, error = true;
String record = "";
System.out.print("Hello, and welcome to the Memo Maker 2015. ");
File memoTitles = new File("Memo Titles.txt");
PrintWriter writeTitles = new PrintWriter(memoTitles);
Scanner scan = new Scanner(System.in);
while(running){
while(error){
System.out.println("\nPlease select an option.");
System.out.println("\n(1) Create a Memo\n(2) View an Old Memo\n(3) Directions for Overwriting\n(4) Exit");
try{
choice = scan.nextInt();
if(choice >4 || choice <1){
System.out.println("\nERROR: Do you see that number there? Okay then... try again.\n");
error = true;
}//close if
else error = false;
}catch(InputMismatchException e){
scan.nextLine();
System.out.println("\nERROR: You need to enter the NUMBER next to your option.\n");
error = true;
} //close catch
}//close try while
error = true;
switch(choice){
case 1:
//create a file
Scanner key = new Scanner(System.in);
System.out.println("Enter the name of your memo.");
name = key.nextLine();
File file = new File(name + ".txt");
record = record + "\n" +file.getName();
writeTitles.print(record);
writeTitles.close();
PrintWriter write = new PrintWriter(file);
System.out.println("\nOkay, now write your memo: ");
memo = key.nextLine();
write.println(memo);
write.close();
System.out.println("\nYour memo has been created.");
break;
case 2:
//view an old file
Scanner readTitles = new Scanner(memoTitles);
Scanner enter = new Scanner(System.in);
if(readTitles.hasNext() == false){
System.out.println("There are no files available. Begin with making a memo.");
running = true;
break;
}//close if
else{
//view a file
System.out.println("Here are your available memos. \nEXACTLY type the name of the memo that you wish to view.\n\n");
System.out.println(record);
while(error){
try{
response = enter.nextLine();
error = false;
}catch(FileNotFoundException e){
System.out.println("\nERROR: ");
error = true;
}
}
error = true;
File trying = new File(response);
Scanner readMemo = new Scanner(trying);
System.out.println("\nHere is the message: ");
while(readMemo.hasNext() ){
String words = readMemo.nextLine();
System.out.println(words);
}//close while
while(error){
System.out.println("\n\nType 1 when you are finished viewing.");
int n = 0;
n = scan.nextInt();
if(n==1){
error = false;
}
else error = true;
}
readMemo.close();
}//close else
break;
case 3:
System.out.println("\nGo to Create a Memo, and type in the name of the file you wish to overwrite.\n");
break;
case 4:
Scanner ans = new Scanner(System.in);
int a = 0;
System.out.println("Are you sure you wish to exit? All of your memos will be lost.");
System.out.println("1. Yes\n2. No");
while (error){
try{
a = ans.nextInt();
if(a != 1 && a !=2){
System.out.println("\nERROR: Please enter a valid option.");
error = true;
}//close if
else error = false;
}catch(InputMismatchException e){
System.out.println("\nERROR: Please enter a valid option.");
error = true;
}//close catch
} //close try while
error = true;
if (a == 1){
System.out.println("Until next time!");
running = false;
}// close if
else {
running = true;
}
}//close switch
}//close running
scan.close();
writeTitles.close();
}//close main
}//close class
这一行,
File trying = new File(response);
即使您可以在 JavaDoc 中看到下面的内容,也不会抛出任何异常,但最好(但这不是强制性的)包含在您的 try...catch
并使用 FileInputStream
(应该包含在 try 块中),因为它是 new FileInputStream()
构造函数抛出异常。在尝试有效读取文件时也删除 Scanner
的使用。
这是一个有用的 link :
- Best way to read a text file in Java
Javadoc
public File(String pathname)
Creates a new File instance by converting
the given pathname string into an abstract pathname. If the given
string is the empty string, then the result is the empty abstract
pathname.
这意味着它不检查文件是否存在。
解决方案
FileInputStream fis = null;
try{
fis = new FileInputStream(new File(enter.nextLine()));
error = false;
} catch (FileNotFoundException e){
// Blabla
}
所以我在尝试使用 try-catch 时遇到了一个奇怪的问题,但它不允许出现异常。我会尽量清楚地描述问题。
一些背景: 好的,任务基本上是允许用户写入和读取一些文件。所以我在我的代码中调用一个以前制作的文件的部分。用户可以输入他们希望调用的文件,因此这个例外当然是强制性的。但是错误说永远不会抛出异常,但是当我 运行 代码时它确实被抛出。让我给你看一些代码。
`while(error){
try{
response = scan.nextLine();
error = false;
}catch(FileNotFoundException e){
System.out.println("\nERROR: ");
error = true;
}
}`
好的,这是我编写的代码。现在这是我的输出(运行ning 没有 try-catch)
This is my output without using a try-catch
这是我的错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unreachable catch block for FileNotFoundException. This exception is never thrown from the try statement body
at fileLab2.MemoMaker.main(MemoMaker.java:126)
希望有人能帮助解决这个问题。我就是为了这个问题才开这个账号的,老实说,挺烦人的。 请询问您是否需要任何说明。
这是我的整个程序:
/*
* tslaurenx
* File reader lab 2
* OOP
* 11/20/15
* Lab 8b
*
*/
package fileLab2;
import java.util.*
import java.io.*;
public class MemoMaker {
public static void main(String[] args) throws IOException {
int choice = 0;
String name = "", response = "", memo = "";
boolean running = true, error = true;
String record = "";
System.out.print("Hello, and welcome to the Memo Maker 2015. ");
File memoTitles = new File("Memo Titles.txt");
PrintWriter writeTitles = new PrintWriter(memoTitles);
Scanner scan = new Scanner(System.in);
while(running){
while(error){
System.out.println("\nPlease select an option.");
System.out.println("\n(1) Create a Memo\n(2) View an Old Memo\n(3) Directions for Overwriting\n(4) Exit");
try{
choice = scan.nextInt();
if(choice >4 || choice <1){
System.out.println("\nERROR: Do you see that number there? Okay then... try again.\n");
error = true;
}//close if
else error = false;
}catch(InputMismatchException e){
scan.nextLine();
System.out.println("\nERROR: You need to enter the NUMBER next to your option.\n");
error = true;
} //close catch
}//close try while
error = true;
switch(choice){
case 1:
//create a file
Scanner key = new Scanner(System.in);
System.out.println("Enter the name of your memo.");
name = key.nextLine();
File file = new File(name + ".txt");
record = record + "\n" +file.getName();
writeTitles.print(record);
writeTitles.close();
PrintWriter write = new PrintWriter(file);
System.out.println("\nOkay, now write your memo: ");
memo = key.nextLine();
write.println(memo);
write.close();
System.out.println("\nYour memo has been created.");
break;
case 2:
//view an old file
Scanner readTitles = new Scanner(memoTitles);
Scanner enter = new Scanner(System.in);
if(readTitles.hasNext() == false){
System.out.println("There are no files available. Begin with making a memo.");
running = true;
break;
}//close if
else{
//view a file
System.out.println("Here are your available memos. \nEXACTLY type the name of the memo that you wish to view.\n\n");
System.out.println(record);
while(error){
try{
response = enter.nextLine();
error = false;
}catch(FileNotFoundException e){
System.out.println("\nERROR: ");
error = true;
}
}
error = true;
File trying = new File(response);
Scanner readMemo = new Scanner(trying);
System.out.println("\nHere is the message: ");
while(readMemo.hasNext() ){
String words = readMemo.nextLine();
System.out.println(words);
}//close while
while(error){
System.out.println("\n\nType 1 when you are finished viewing.");
int n = 0;
n = scan.nextInt();
if(n==1){
error = false;
}
else error = true;
}
readMemo.close();
}//close else
break;
case 3:
System.out.println("\nGo to Create a Memo, and type in the name of the file you wish to overwrite.\n");
break;
case 4:
Scanner ans = new Scanner(System.in);
int a = 0;
System.out.println("Are you sure you wish to exit? All of your memos will be lost.");
System.out.println("1. Yes\n2. No");
while (error){
try{
a = ans.nextInt();
if(a != 1 && a !=2){
System.out.println("\nERROR: Please enter a valid option.");
error = true;
}//close if
else error = false;
}catch(InputMismatchException e){
System.out.println("\nERROR: Please enter a valid option.");
error = true;
}//close catch
} //close try while
error = true;
if (a == 1){
System.out.println("Until next time!");
running = false;
}// close if
else {
running = true;
}
}//close switch
}//close running
scan.close();
writeTitles.close();
}//close main
}//close class
这一行,
File trying = new File(response);
即使您可以在 JavaDoc 中看到下面的内容,也不会抛出任何异常,但最好(但这不是强制性的)包含在您的 try...catch
并使用 FileInputStream
(应该包含在 try 块中),因为它是 new FileInputStream()
构造函数抛出异常。在尝试有效读取文件时也删除 Scanner
的使用。
这是一个有用的 link :
- Best way to read a text file in Java
Javadoc
public File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.
这意味着它不检查文件是否存在。
解决方案
FileInputStream fis = null;
try{
fis = new FileInputStream(new File(enter.nextLine()));
error = false;
} catch (FileNotFoundException e){
// Blabla
}