当我尝试编译Java代码时,为什么会出现“Exception;必须被捕获或声明被抛出”?

时间:2009-05-26 02:22:57

标签: java exception

考虑:

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.io.*;


public class EncryptURL extends JApplet implements ActionListener {

    Container content;
    JTextField userName = new JTextField();
    JTextField firstName = new JTextField();
    JTextField lastName = new JTextField();
    JTextField email = new JTextField();
    JTextField phone = new JTextField();
    JTextField heartbeatID = new JTextField();
    JTextField regionCode = new JTextField();
    JTextField retRegionCode = new JTextField();
    JTextField encryptedTextField = new JTextField();

    JPanel finishPanel = new JPanel();


    public void init() {

        //setTitle("Book - E Project");
        setSize(800, 600);
        content = getContentPane();
        content.setBackground(Color.yellow);
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        JButton submit = new JButton("Submit");

        content.add(new JLabel("User Name"));
        content.add(userName);

        content.add(new JLabel("First Name"));
        content.add(firstName);

        content.add(new JLabel("Last Name"));
        content.add(lastName);

        content.add(new JLabel("Email"));
        content.add(email);

        content.add(new JLabel("Phone"));
        content.add(phone);

        content.add(new JLabel("HeartBeatID"));
        content.add(heartbeatID);

        content.add(new JLabel("Region Code"));
        content.add(regionCode);

        content.add(new JLabel("RetRegionCode"));
        content.add(retRegionCode);

        content.add(submit);

        submit.addActionListener(this);
    }


    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand() == "Submit"){

            String subUserName = userName.getText();
            String subFName = firstName.getText();
            String subLName = lastName.getText();
            String subEmail = email.getText();
            String subPhone = phone.getText();
            String subHeartbeatID = heartbeatID.getText();
            String subRegionCode = regionCode.getText();
            String subRetRegionCode = retRegionCode.getText();

            String concatURL =
                "user=" + subUserName + "&f=" + subFName +
                "&l=" + subLName + "&em=" + subEmail +
                "&p=" + subPhone + "&h=" + subHeartbeatID +
                "&re=" + subRegionCode + "&ret=" + subRetRegionCode;

            concatURL = padString(concatURL, ' ', 16);
            byte[] encrypted = encrypt(concatURL);
            String encryptedString = bytesToHex(encrypted);
            content.removeAll();
            content.add(new JLabel("Concatenated User Input -->" + concatURL));

            content.add(encryptedTextField);
            setContentPane(content);
        }
    }

    public static byte[] encrypt(String toEncrypt) throws Exception{
        try{
            String plaintext = toEncrypt;
            String key = "01234567890abcde";
            String iv = "fedcba9876543210";

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());

            return encrypted;
        }
        catch(Exception e){
        }
    }


    public static byte[] decrypt(byte[] toDecrypt) throws Exception{
        String key = "01234567890abcde";
        String iv = "fedcba9876543210";

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
        byte[] decrypted = cipher.doFinal(toDecrypt);

        return decrypted;
    }


    public static String bytesToHex(byte[] data) {
        if (data == null)
        {
            return null;
        }
        else
        {
            int len = data.length;
            String str = "";
            for (int i=0; i<len; i++)
            {
                if ((data[i]&0xFF) < 16)
                    str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
                else
                    str = str + java.lang.Integer.toHexString(data[i]&0xFF);
            }
            return str;
        }
    }


    public static String padString(String source, char paddingChar, int size)
    {
        int padLength = size-source.length() % size;
        for (int i = 0; i < padLength; i++) {
            source += paddingChar;
        }
        return source;
    }
}

我收到一个未报告的例外:

java.lang.Exception; must be caught or declared to be thrown
byte[] encrypted = encrypt(concatURL);

以及:

.java:109: missing return statement

如何解决这些问题?

6 个答案:

答案 0 :(得分:31)

你所有的问题都来自于这个

byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
return encrypted;

包含在try,catch块中的问题是,如果程序发现异常,则表示您没有返回任何内容。像这样(将其修改为程序逻辑):

public static byte[] encrypt(String toEncrypt) throws Exception{
    try{
        String plaintext = toEncrypt;
        String key = "01234567890abcde";
        String iv = "fedcba9876543210";

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE,keyspec,ivspec);
        byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());

        return encrypted;
    } catch(Exception e){
        return null;            // Always must return something
    }
}

对于第二个,您必须从 encrypt 方法调用中捕获异常,如下所示(也可以将其修改为程序逻辑):

public void actionPerformed(ActionEvent e)
  .
  .
  .
    try {
        byte[] encrypted = encrypt(concatURL);
        String encryptedString = bytesToHex(encrypted);
        content.removeAll();
        content.add(new JLabel("Concatenated User Input -->" + concatURL));

        content.add(encryptedTextField);
    setContentPane(content);
    } catch (Exception exc) {
        // TODO: handle exception
    }
}

您必须从中吸取教训:

  • 带有返回类型的方法必须始终返回该类型的对象,我的意思是在所有可能的场景中
  • 所有已检查的例外情况必须始终处理

答案 1 :(得分:6)

问题出在这个方法中:

  public static byte[] encrypt(String toEncrypt) throws Exception{

这是 方法签名 ,它几乎说:

  • 方法名称是什么:加密
  • 它接收的参数:名为 toEncrypt
  • 的字符串
  • 其访问修饰符:公共静态
  • 如果在调用时可能抛出异常。

在这种情况下,方法签名表示在调用时,此方法“可能”可能会抛出“异常”类型的异常。

    ....
    concatURL = padString(concatURL, ' ', 16);
    byte[] encrypted = encrypt(concatURL); <-- HERE!!!!!
    String encryptedString = bytesToHex(encrypted);
    content.removeAll();
    ......

所以编译器说:要么用try / catch构造包围它,要么声明方法(正在使用的地方)自己抛出“Exception”。

真正的问题是“加密”方法定义。没有方法应该返回“异常”,因为它太通用了,可能隐藏其他一些kinds of exception更好的是有一个特定的异常。

试试这个:

public static byte[] encrypt(String toEncrypt) {
    try{
      String plaintext = toEncrypt;
      String key = "01234567890abcde";
      String iv = "fedcba9876543210";

      SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
      IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

      Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
      cipher.init(Cipher.ENCRYPT_MODE,keyspec,ivspec);
      byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());

      return encrypted;
    } catch ( NoSuchAlgorithmException nsae ) { 
        // What can you do if the algorithm doesn't exists??
        // this usually won't happen because you would test 
        // your code before shipping. 
        // So in this case is ok to transform to another kind 
        throw new IllegalStateException( nsae );
    } catch ( NoSuchPaddingException nspe ) { 
       // What can you do when there is no such padding ( whatever that means ) ??
       // I guess not much, in either case you won't be able to encrypt the given string
        throw new IllegalStateException( nsae );
    }
    // line 109 won't say it needs a return anymore.
  }

基本上在这种特殊情况下,您应该确保加密包在系统中可用。

Java需要加密包的扩展,因此,异常被声明为“已检查”的异常。当你不在场时你可以处理。

在这个小程序中,如果加密包不可用,则无法执行任何操作,因此请在“开发”时检查该程序。如果在程序运行时抛出这些异常是因为你在“开发”中做错了,那么RuntimeException子类更合适。

最后一行不再需要return语句,在第一个版本中你捕获异常并且什么都不做,这是错误的。

try { 
    // risky code ... 
} catch( Exception e ) { 
    // a bomb has just exploited
    // you should NOT ignore it 
} 

// The code continues here, but what should it do???

如果代码失败,最好Fail fast

以下是一些相关的答案:

答案 2 :(得分:5)

第一个错误

  

java.lang.Exception的;必须被捕获或声明被抛出byte [] encrypted = encrypt(concatURL);

表示您的encrypt方法抛出了一个异常,该异常未被您调用它的actionPerformed方法处理或声明。请在Java Exceptions Tutorial了解所有相关信息。

您可以选择几个选项来获取要编译的代码。

  • 您可以从throws Exception方法移除encrypt实际处理 encrypt内的例外。
  • 您可以从encrypt移除try / catch块,并将throws Exception和异常处理块添加到actionPerformed方法。

通常最好在最低级别处理异常,而不是将其传递到更高级别。

第二个错误只是意味着您需要将return语句添加到包含第109行的任何方法(在这种情况下也是encrypt)。方法中有一个return语句,但如果抛出异常,则可能无法访问,因此您需要返回catch块,或者从encrypt中删除try / catch,如前所述。

答案 3 :(得分:1)

您需要决定如何处理encrypt方法引发的异常。

目前,encryptthrows Exception一起声明 - 但是,在方法体中,异常会在try / catch块中捕获。我建议你:

  • throws Exception中删除encrypt子句并在内部处理异常(考虑至少编写一条日志消息);或者,
  • encrypt的正文中删除try / catch块,然后使用try / catch将调用包围到encrypt(即actionPerformed )。

关于编译错误,请参阅:如果在try encrypt块中抛出异常,则在catch块完成后不会返回任何内容。您可以通过最初将返回值声明为null

来解决此问题
public static byte[] encrypt(String toEncrypt) throws Exception{
  byte[] encrypted = null;
  try {
    // ...
    encrypted = ...
  }
  catch(Exception e){
    // ...
  }
  return encrypted;
}

但是,如果你能纠正更大的问题(异常处理策略),这个问题就会自行处理 - 特别是如果你选择我建议的第二个选项。

答案 4 :(得分:0)

actionPerformed(ActionEvent e)中,您致电encrypt(),宣布投放Exception。但是,actionPerformed既没有捕获此异常(使用try / catch来调用encrypt())也没有声明它会抛出Exception本身。

但是,encrypt方法并没有真正抛出Exception。它吞没了所有异常,甚至没有记录投诉。 (糟糕的做法和糟糕的风格!)

此外,您的encrypt方法会执行以下操作:

public static byte[] encrypt(String toEncrypt) throws Exception {
  try{
    ....
    return encrypted; // HERE YOU CORRECTLY RETURN A VALUE
  } catch(Exception e) {
  }
  // YOU DO NOT RETURN ANYTHING HERE
}

也就是说,如果你捕获任何异常,你会默默地丢弃它,然后从encrypt方法的底部掉下来,而不实际返回任何内容。这不会编译(如您所见),因为声明为返回值的方法必须返回一个值或为每个可能的代码路径抛出一个Exception。

答案 5 :(得分:0)

在你的'encrypt'方法中,你应该摆脱try / catch,而是在你调用encrypt的地方添加一个try / catch(在'actionPerformed'里面)或者在encrypt中的catch里面返回null(这是第二个)错误。