Jacksum NoSuchAlgorithmException [英] Jacksum NoSuchAlgorithmException
问题描述
I'm trying to use the Jacksum API to generate a Whirlpool hash, but I'm getting a NoSuchAlgorithmException:
import java.security.NoSuchAlgorithmException;
import jonelo.jacksum.JacksumAPI;
import jonelo.jacksum.algorithm.AbstractChecksum;
public static String genHash(String inText) {
AbstractChecksum checksum = null;
checksum = JacksumAPI.getChecksumInstance("whirlpool");
checksum.update(inText.getBytes());
return checksum.getFormattedValue();
}
我尝试了其他流行的算法(sha256,md5),它们显然都不是这样".
I tried other popular algorithms (sha256, md5) and they all apparently "aren't such".
./libsdpg.java:27: error: unreported exception NoSuchAlgorithmException; must be caught or declared to be thrown
checksum = JacksumAPI.getChecksumInstance("whirlpool");
^
1 error
我添加了try-catch,现在它实际上正在获取错误.
I added the try-catch, and now it's actually getting the error.
推荐答案
您实际上并不是在获取"异常.编译器告诉您您未能适当地处理已检查的异常.
You are not actually "getting" an exception. The compiler is telling you that you have failed to appropriately handle a checked exception.
JacksumAPI#getChecksumInstance(java.lang.String)
方法引发一个称为NoSuchAlgorithmException
的已检查异常.必须(使用try-catch
)显式处理已检查的异常,或者封闭方法必须声明将其包括在其签名中,从而引发该异常.因此,您的选择是:
The JacksumAPI#getChecksumInstance(java.lang.String)
method throws a checked exception called NoSuchAlgorithmException
. A checked exception must either be explicitly handled (using try-catch
), or the enclosing method must declare that it throws it by including it in its signature. So your options are:
try {
...
checksum = JacksumAPI.getChecksumInstance("whirlpool");
...
} catch(NoSuchAlgorithmException e) {
//handle the exception
}
或将您的方法签名更改为:
or change your method signature to:
public static String genHash(String inText) throws NoSuchAlgorithmException {
...
}
请记住,使用第二个选项,您只是将处理推到了更高的层次(即,调用genHash
的地方);您基本上必须在某个时候处理它.
Keep in mind with the second option you have merely pushed the handling up to a higher level (i.e., where genHash
is called); you will essentially have to handle it at some point.
这篇关于Jacksum NoSuchAlgorithmException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!