未报告的异常java.lang.exception [英] Unreported exception java.lang.exception

查看:161
本文介绍了未报告的异常java.lang.exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未报告的异常java.lang.exception:必须被捕获或声明为throw。
为什么会出现这个问题?这是一个有助于解决这个问题的简单方法吗?

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?

我在我的java中应用这个代码..

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

错误显示在以下部分

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

有什么想法吗?
我用java netbeans来做我的项目..

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

推荐答案

你的加密()方法抛出异常。这意味着,在您调用此方法的地方,您应该明确地抛出此异常或使用 try-catch 阻止。

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

您应该将其括在:

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

或声明用括起此代码的方法抛出异常

此外,这是关于异常传播指南(Java)中的另一篇有趣的读物

这篇关于未报告的异常java.lang.exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆