I'm developing a public API for people to use in their applications. I'm currently trying to figure out the best way to handle exceptions. For example, the following code snippet throws four different kinds of exceptions:
Signature s = Signature.getInstance("SHA1withRSA"); //NoSuchAlgorithmException
s.initSign(keyChain.getPrivateKey()); //InvalidKeyException
s.update(plainText.getBytes("UTF-8")); //UnsupportedEncodingException
byte[] signature = s.sign(); //SignatureException
String signedData = base64Encoder.encode(signature);
My question is, how do I handle these in the best possible way?
One way I came up with was catching exceptions and throwing a custom exception:
public void signRequest(Request request) throws APISignatureException {
try {
...
Signature s = Signature.getInstance("SHA1withRSA"); //NoSuchAlgorithmException
s.initSign(keyChain.getPrivateKey()); //InvalidKeyException
s.update(plainText.getBytes("UTF-8")); //UnsupportedEncodingException
byte[] signature = s.sign(); //SignatureException
String signedData = base64Encoder.encode(signature);
...
}
catch (Exception e) {
throw new APISignatureException("Failed to create signature", e);
}
}
Is this a good way of handling exceptions for an open API?
