gpt4 book ai didi

java - 不允许使用文件未找到异常,但这是必要的

转载 作者:行者123 更新时间:2023-12-01 06:11:29 27 4
gpt4 key购买 nike

所以我遇到了一个奇怪的问题,我试图使用 try-catch,但异常(exception)情况不允许。我会尽量清楚地描述这个问题。

一些背景:好的,所以任务基本上是允许用户写入一些文件并读取它们。因此,我在代码中调用以前创建的文件的部分。用户可以输入他们想要调用的文件,因此当然这个异常(exception)是强制性的。但错误表明永远不会抛出异常,但是当我运行代码时,它确实会被抛出。让我向您展示一些代码。

`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

这是我的错误:

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 block 内)因为它是 new FileInputStream() 构造函数抛出异常。在尝试高效读取文件时也不要使用 Scanner

这是一个有用的链接:

<小时/>

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
}

关于java - 不允许使用文件未找到异常,但这是必要的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33840432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com