- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我遇到了一个奇怪的问题,我试图使用 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
。
这是一个有用的链接:
<小时/>
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/
我正在使用 this solution在二进制矩阵中找到与图像边界对齐的矩形。假设现在我想找到一个不与图像边框对齐的矩形,并且我不知道它的方向;找到它的最快方法是什么? 为了示例,让我们寻找一个仅包含
else: 行在这个 Python 程序中是否正确/必要? from random import randrange for n in range(10): r = randrange(0,1
在 TDPL 7.1.5.1 中讨论了将 Widget w2 分配给 w1 并且作者指出“将 w2 逐个字段分配给 w1 会将 w2.array 分配给 w1.array——一个简单的数组边界分配,而
我是一名优秀的程序员,十分优秀!