0

Unreported exception java.lang.exception : Must be caught or declared to be throw. Why this problem will occur? Is it some simple method that can help to solve this problems?

I apply this code in my 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");
}

The error is shown in this below part

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

Any idea? Im using java netbeans to do my project..

user1493222
  • 19
  • 2
  • 4

2 Answers2

4

Your encrypt() method throws an Exception. This means that where you're calling this method, you should explictly throw this Exception or handle it using a try-catch block.

In your case, for this particular code:

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

You should either enclose it in:

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

or declare the method where this code is enclosed with throws Exception.

Community
  • 1
  • 1
Sujay
  • 6,753
  • 2
  • 30
  • 49
0

1. There are 2 ways to handle the exception.

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

2. encrypt() method above throws an Exception

So either declare it on the method declaration in which you are calling it.

eg:

public void MyCallingMethod() throws Exception{

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


}

Or handle it with try/catch block, finally is optional

try{

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


  }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75