为什么我一直得到“必须被抓住或宣布被抛出”错误? [英] Why do I keep getting the "must be caught or declared to be thrown" error?

查看:128
本文介绍了为什么我一直得到“必须被抓住或宣布被抛出”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

import java.io.*;
import java.util.*;
import java.util.Scanner;
import java.io.PrintWriter;

public class EncryptionDecryption {

public static void main(String[] args) throws java.io.IOException{

int z = getRandom();
boolean luck = true;

while(luck == true){
String codeString = getString();
System.out.println(codeString);

char[] enCharArray = encrypt(codeString, z);
String encryptedString = new String(enCharArray);
System.out.println(encryptedString);

char[] deCharArray = decrypt(encryptedString, z);
String decryptedString = new String(deCharArray);
System.out.println(decryptedString);
putString(encryptedString);

if(codeString.length() == 0)
luck = false;
}
}

static String getString(){
Scanner input = new Scanner(new File(" "));
String codeString = input.next();
return codeString;
}

static void putString (String finalString){
PrintWriter work = new PrintWriter("EncryptedDocument.txt");
work.print(finalString + " ");
work.close();
}

static char[] encrypt(String encryptString, int z){
char[] codeChar = encryptString.toCharArray();
char[] enCharArray;
enCharArray = new char[codeChar.length];
for(int i = 0; i < codeChar.length; i++){
int x = codeChar[i];
int enInt = encryptChar(x, z);
char enChar = (char)enInt;
enCharArray[i] = enChar;
if(x == 32){
enInt = 32;
enChar = (char)enInt;
enCharArray[i] = enChar;
}
}
return enCharArray;
}

static char[] decrypt(String decryptString, int z){
char[] deCodeChar = decryptString.toCharArray();
char[] deCharArray;
deCharArray = new char[deCodeChar.length];
for(int i = 0; i < deCodeChar.length; i++){
int x = deCodeChar[i];
int deInt = decryptChar(x, z);
char deChar = (char)deInt;
deCharArray[i] = deChar;
if(x == 32){
deInt = 32;
deChar = (char)deInt;
deCharArray[i] = deChar;
}
}
return deCharArray;
}

static int encryptChar(int x, int z){
int y = 'A';
int enInt = (x - y + z) % 26 + y;
return enInt;
}

static int decryptChar(int x, int z){
int y = 'A';
int deInt = (x - y + 104 - z) % 26 + y;
return deInt;
}

static int getRandom(){
int encryptMethod = 0;
while(encryptMethod == 0){
Random encrypt = new Random();
encryptMethod = encrypt.nextInt(96);
}
return encryptMethod;
}

}

我一直收到这些错误尝试编译:

I keep getting these errors when i try to compile:

EncryptionDecryption.java:32: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Scanner input = new Scanner(new File(" "));
                ^
EncryptionDecryption.java:38: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
PrintWriter work = new PrintWriter("EncryptedDocument.txt");
                   ^
2 errors


推荐答案

因为您调用一个方法来声明它抛出一个FileNotFoundException,并且您没有捕获异常,也没有声明封闭方法抛出它。这在Java中是不允许的。必须捕获所有已检查的异常,或者在方法的 throws 子句中声明:

Because you call a method that declares that it throws a FileNotFoundException, and you don't catch the exception, nor do you declare that the enclosing method throws it. This is not allowed in Java. All checked exceptions must either be caught, or declared in the throws clause of the method:

static String getString() throws FileNotFoundException {

如果你可以处理异常并做一些有意义的东西,让你的程序继续按预期工作,然后捕获异常。如果你不能在这个方法中处理它,那么让你的方法的调用者为你处理它,并通过在throws子句中声明它来传播它。

If you can handle the exception and do something meaningful that makes you program continue to work as expected, then catch the exception. If you can't handle it in this method, then let the caller of your method handle it for you, and let it propagate by declaring it in the throws clause.

这篇关于为什么我一直得到“必须被抓住或宣布被抛出”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆