Trabla: Java & MessageDigest: error: unreported exception NoSuchAlgorithmException; must be caught or declared to be thrown
Trabla Code:
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class Main {
public static void main(String[] args) {
MessageDigest md = null;
md = MessageDigest.getInstance("MD5");
}
}
Solving:
Wrap MessageDigest.getInstance with try/catch NoSuchAlgorithmException
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class Main {
public static void main(String[] args) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
//DO YOUR STAFF
System.out.println("Hooray!!! ALL is PERFECT!!! :)");
} catch (java.security.NoSuchAlgorithmException e) {
System.out.println(e.getMessage());
}
}
}
No comments:
Post a Comment