catch {throw ..}实现后缺少返回语句

时间:2019-02-01 07:24:06

标签: android passwords keystore

即使在try块中返回,它也表示缺少return语句。我不明白。 我正在尝试制作一个android应用程序,并且还使用android keystore存储该应用程序的登录凭据。 另外,如果有人可以给我提供简单的keystore实现示例,那就太好了。我发现了2个示例,它们不是很容易理解(缺少代码),并且也很难实现我的情况。

private SecretKey createKey() {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            keyGenerator.init(new KeyGenParameterSpec.Builder("Key", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                    .setUserAuthenticationRequired(true)            //burayı kaldırırsan screen locka gerek kalmaz
                    .setUserAuthenticationValidityDurationSeconds(5)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                    .build());
            return keyGenerator.generateKey();
        }
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
        throw new RuntimeException("Failed to create a symmetric key", e);
    }

}

3 个答案:

答案 0 :(得分:4)

问题是,如果您的Build.VERSION.SDK_INT >= Build.VERSION_CODES.M条件没有得到满足,您就没有回报。

private SecretKey createKey() {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // ...
            return keyGenerator.generateKey();
        }
        // add a return here if we're not on >= Android M.
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
        throw new RuntimeException("Failed to create a symmetric key", e);
    }
    // you could also have a return here.
}

答案 1 :(得分:1)

如果if语句不符合要求,则需要运行return语句。现在,只有在SDK大于Build.VERSION_CODES.M时,您才返回某些内容。

您可以在整个try and catch之后放置一个return语句,因为如果您在try中返回,它将离开该方法。这意味着只有在构建SDK不符合要求时,才会运行其他返回。

private SecretKey createKey() {
try {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        keyGenerator.init(new KeyGenParameterSpec.Builder("Key", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)            //burayı kaldırırsan screen locka gerek kalmaz
                .setUserAuthenticationValidityDurationSeconds(5)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        return keyGenerator.generateKey();
    }
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
    throw new RuntimeException("Failed to create a symmetric key", e);
}

// Return here...

}

答案 2 :(得分:1)

在最后一个花括号之前添加return语句

相关问题