所以我有一个奇怪的问题,我试图使用try-catch,但它不允许的例外。我会尽可能清楚地描述这个问题。
一些背景: 好的,所以赋值是基本上允许用户写一些文件并读取它们。所以我在我的代码中我调用以前创建的文件的部分。用户可以输入他们想要调用的文件,因此当然这个例外是强制性的。但是错误说永远不会抛出异常,但是当我运行代码时它会被抛出。让我给你看一些代码。
`while(error){
try{
response = scan.nextLine();
error = false;
}catch(FileNotFoundException e){
System.out.println("\nERROR: ");
error = true;
}
}`
好的,这是我的书面代码。现在这是我的输出(没有try-catch运行)
This is my output without using a try-catch
这是我的错误:
线程中的异常" main" java.lang.Error:未解析的编译 问题:FileNotFoundException的无法访问的catch块。这个 永远不会从try语句主体
抛出异常在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
答案 0 :(得分:-1)
这一行,
File trying = new File(response);
即使你可以在 JavaDoc 中看到下面的内容,也不会抛出任何异常(但不是强制性的)包含在try...catch
中并使用{ {3}}(应该包含在try块中),因为它是抛出异常的new FileInputStream()
构造函数。在尝试有效读取文件时也删除Scanner
的使用。
这是一个有用的链接:
public File(String pathname)
通过转换创建一个新的File实例 给定的路径名字符串为抽象路径名。如果给定 string是空字符串,然后结果是空的抽象 路径名。
这意味着它不会检查文件是否存在。
FileInputStream fis = null;
try{
fis = new FileInputStream(new File(enter.nextLine()));
error = false;
} catch (FileNotFoundException e){
// Blabla
}