程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了当我试图编译Java代码时,为什么会得到“Exception;must be catched or declared to be throw”?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决当我试图编译Java代码时,为什么会得到“Exception;must be catched or declared to be throw”??

开发过程中遇到当我试图编译Java代码时,为什么会得到“Exception;must be catched or declared to be throw”?的问题如何解决?下面主要结合日常开发的经验,给出你关于当我试图编译Java代码时,为什么会得到“Exception;must be catched or declared to be throw”?的解决方法建议,希望对你解决当我试图编译Java代码时,为什么会得到“Exception;must be catched or declared to be throw”?有所启发或帮助;

你所有的问题都源于此

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

包含在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
    }
}
@H_197_6@

对于第二个,你必须从crypto@H_197_6@方法调用中捕获Exception@H_197_6@ ,如下所示(也可以在程序逻辑上对其进行修改):

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
    }
}
@H_197_6@

你必须从中吸取教训:

具有返回类型的方法必须始终返回该类型的对象,我的意思是在所有可能的情况下 必须始终处理所有检查的异常

解决方法

虑:

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,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;
    }
}
@H_197_6@

我收到了一个未报告的异常:

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

以及:

.java:109: missing return statement
@H_197_6@

我该如何解决这些问题?

大佬总结

以上是大佬教程为你收集整理的当我试图编译Java代码时,为什么会得到“Exception;must be catched or declared to be throw”?全部内容,希望文章能够帮你解决当我试图编译Java代码时,为什么会得到“Exception;must be catched or declared to be throw”?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。