簡體   English   中英

未報告的異常java.lang.exception

[英]Unreported exception java.lang.exception

未報告的異常java.lang.exception:必須被捕獲或聲明為拋出。 為什么會出現此問題? 是否有一些簡單的方法可以幫助解決此問題?

我在我的java中應用此代碼。

public byte[] encrypt(String message) throws Exception {
    MessageDigest md = MessageDigest.getInstance("md5");
    byte[] digestOfPassword = md.digest("ABCDEABCDE"
                    .getBytes("utf-8"));
    byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
    }

    SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);

    byte[] plainTextBytes = message.getBytes("utf-8");
    byte[] cipherText = cipher.doFinal(plainTextBytes);
    // String encodedCipherText = new sun.misc.BASE64Encoder()
    // .encode(cipherText);

    return cipherText;
}

public String decrypt(byte[] message) throws Exception {
    MessageDigest md = MessageDigest.getInstance("md5");
    byte[] digestOfPassword = md.digest("ABCDEABCDE"
                    .getBytes("utf-8"));
    byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
    }

    SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    IvParameterSpec iv = new IvParameterSpec(new byte[8]);
    Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);

    byte[] plainText = decipher.doFinal(message);

    return new String(plainText, "UTF-8");
}

錯誤顯示在下面的這一部分

byte[] pass = encrypt(password);
String pw = new String(pass);

任何想法? 我用java netbeans做我的項目。

您的encrypt()方法拋出Exception 這意味着在調用此方法時,應明確拋出此Exception或使用try-catch塊對其進行處理。

對於您的情況,對於此特定代碼:

byte[] pass = encrypt(password);
String pw = new String(pass);

您應該將其包含在:

try{
 byte[] pass = encrypt(password);
 String pw = new String(pass);
}catch(Exception exe){
 //Your error handling code
}

或聲明將代碼包含在throws Exception

1.兩種處理異常的方法。

 - Either `declare` it
 - or `Handle` it.

2.上面的crypto encrypt()方法拋出一個異常

因此,可以在調用它的方法聲明中聲明它。

例如:

public void MyCallingMethod() throws Exception{

     byte[] pass = encrypt(password);
     String pw = new String(pass);


}

或使用 try/catch 處理它 finally 是可選的

try{

     byte[] pass = encrypt(password);
     String pw = new String(pass);
   }catch(Exception ex){


  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM