为什么我一直得到“必须被抓或被宣布被抛出”的错误?

这是我的代码

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; } } 

我在尝试编译时不断收到这些错误:

 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,并且您没有捕获exception,也没有声明封闭方法抛出它。 这在Java中是不允许的。 必须捕获所有已检查的exception,或者在方法的throws子句中声明:

 static String getString() throws FileNotFoundException { 

如果您可以处理exception并执行一些有意义的操作,使程序继续按预期工作,则捕获exception。 如果你不能在这个方法中处理它,那么让你的方法的调用者为你处理它,并通过在throws子句中声明它来传播它。

在方法getString()您正在创建一个new File() ,它会抛出FileNotFoundException 。 必须通过将扫描程序代码块包含在try-catch块中或由方法抛出的声明来捕获此FileNotFoundException 。 同样的事情适用于putString (String finalString)方法。

你的代码应该是

 static String getString(){ Scanner input; try { input = new Scanner(new File(" ")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String codeString = input.next(); return codeString; } static void putString (String finalString){ PrintWriter work; try { work = new PrintWriter("EncryptedDocument.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } work.print(finalString + " "); work.close(); } 

注意你有……

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

在那里“投掷”线? 这是告诉Java当发生IOException时,它应该被传递给任何名为main()

Java要求您针对可能引发的任何可能exception执行此操作。 这些错误消息告诉您的是,您的代码中可能会引发另外两种类型的exception; 你需要指定它们被抛出(或者通过try / catch块自己处理它们)。

尝试添加一个

 throws java.io.IOException 

任何使用文件IO的方法。 我认为它会解决你的问题

因为FileNotFoundException是一个经过检查的exception ,这意味着在Java中你必须在它们发生时对它们做些什么。

您有两种处理已检查exception的选项:

  • 抓住他们并用他们做点什么try{ } catch(FileNotFoundException e) { //do something with e}

  • 重新抛出它们,这意味着添加行throws FileNotFoundException添加方法签名的结束

在上面的代码中, Scanner input = new Scanner(new File(" ")); 指示您正在尝试打开文件。当您尝试打开文件时,这可能会也可能不可用。如果在那时它不可用,则抛出exception。因此,始终建议相应地处理此代码。 。 must be caught or declared to be thrown表明我们可能期望上面解释的exception可以被代码抛出,因此必须处理。exception处理使用

1.尝试抓住

 try{ ur code that generates exception } catch{ handle exception } 

这说明了first part of the exception you got "must be caught"first part of the exception you got "must be caught"

扔掉

当你不想在这里处理exception时,可以将它抛给调用它的方法。调用方法必须处理这个exception。

 meth1() { meth2(); } meth2()throws Exception { try{ ..... ..... } catch(Exception e) { ... u will not handle it here; throw e; //exception thrown to the calling method meth1() } } 

这说明了

 second part of the exception "declared to be thrown"