Why do I keep getting the “must be caught or decla

2019-01-29 08:31发布

问题:

Here is my code

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

回答1:

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 {

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.



回答2:

In the method getString() You are creating a new File(), which throws FileNotFoundException. This FileNotFoundException must be caught by enclosing the scanner code block with in the try-catch block or declared thrown by the method. Same thing applies to the putString (String finalString) method.

Your code should be

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


回答3:

Notice how you have...

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

with that "throws" line there? That's to tell Java that when an IOException occurs, it should be passed up to whatever called main().

Java requires that you do this for any possible exception that might be raised. What those error messages are telling you is that there are 2 other types of exceptions that might be raised in your code; you need to specify that they're thrown (or otherwise handle them yourself, via a try/catch block).



回答4:

try adding a

throws java.io.IOException

to any METHOD that uses file IO as well. I think it will solve your problem



回答5:

Because FileNotFoundException is a checked Exception which means in Java that you have to do something with them when they occur.

You have two options to handle checked exceptions:

  • catch them and do something with them try{ } catch(FileNotFoundException e) { //do something with e}

  • re-throw them, which means add the line throws FileNotFoundException add the end of your method signature



回答6:

In the code above Scanner input = new Scanner(new File(" ")); indicate that you are trying to open a file.Now this might or might not be available when you are trying to open it.Hence a exception is thrown if it is not available at that time.Hence it is always recommended to handle this code correspondingly. must be caught or declared to be thrown indicate that we may expect an exception such as above explained can be thrown by the code and hence must be handled.Exception handling is done using either

1.use try catch

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

This illustrates the first part of the exception you got "must be caught"

2.use throw

when you dont want to handle the exception right here you can throw it to the method that is calling it.The calling method must handle this 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()
}
}

This illustrates the

second part of the exception "declared to be thrown"