1

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

}

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

EDIT: I added the try-catch, and now it's actually getting the error.

2 Answers 2

3

You are not actually "getting" an exception. The compiler is telling you that you have failed to appropriately handle a checked exception.

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 {
    ...
}

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I added the try-catch and now the error is actually happening.
Well, not a compiler error, but the exception is run (//handle the exception) in your code
So when running you are getting an exception during runtime? Then that's because you have passed in an invalid argument. Perhaps "whirlpool" is not a proper argument? I would read the documentation for Jacksum.
I think I forgot to recompile after changing the algorithm back to whirlpool (I'm new to Java). Working fine now.
1

You are not getting the NoSuchAlgorithmException. Instead the compiler is saying that the getChecksumInstance() throws a checked exception, NoSuchAlgorithmException which needs to be handled, since you've not already done that.

You can do that either by having a throws clause in your genHash()(you'll need to handle the exception in the method where genHash() is called though)

// Solution 1
public static String genHash(String inText) throws NoSuchAlgorithmException {

or by surrounding the call to getChecksumInstance() within a try-catch.

// Solution 2
try {
    checksum = JacksumAPI.getChecksumInstance("whirlpool");
} catch(NoSuchAlgorithmException e) {
    // Do something on exception
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.