Reputation: 5713
I have code snippets:
try {
AlgorithmParameterSpec spec = null;
synchronized (lock) {
// ...
KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA_ALGORITHM, PROVIDER_ANDROID_KEY_STORE);
generator.initialize(spec);
generator.generateKeyPair();
}
} catch (Throwable e) {
Logger.e("Exception " + e.getMessage() + " occurred",e);
}
On devices with SDK version < 23 I get java.security.ProviderException
in line generator.generateKeyPair();
and Application Crashes!
My question is: why Application crashes if I have try/catch?
From this answer: Why we don't have to add try-catch to a RuntimeException?
That's because it's an unchecked exception. It doesn't need to be explicitly declared or catched. Also see the Sun tutorial on the subject.
but I don't think its my case
My stack trace:
8:28:39.700 9359-10032/com.hoo.app.dev E/art: JNI DETECTED ERROR IN APPLICATION: JNI FindClass called with pending exception 'java.security.ProviderException' thrown in unknown throw location
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: in call to FindClass
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: from byte[] com.google.android.gms.org.conscrypt.NativeCrypto.EVP_DigestSignFinal(com.google.android.gms.org.conscrypt.NativeRef$EVP_MD_CTX)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: "pool-7-thread-1" prio=5 tid=66 Runnable
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: | group="main" sCount=0 dsCount=0 obj=0x12da8400 self=0x7f87147800
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: | sysTid=10032 nice=0 cgrp=default sched=0/0 handle=0x7f9206a000
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: | state=R schedstat=( 60197617 2542383 67 ) utm=6 stm=0 core=1 HZ=100
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: | stack=0x7f646fe000-0x7f64700000 stackSize=1036KB
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: | held mutexes= "mutator lock"(shared held)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.google.android.gms.org.conscrypt.NativeCrypto.EVP_DigestSignFinal(Native method)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.google.android.gms.org.conscrypt.OpenSSLSignature.engineSign(:com.google.android.gms:224)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.security.Signature$SignatureImpl.engineSign(Signature.java:672)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.security.Signature.sign(Signature.java:381)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.android.org.bouncycastle.x509.X509Util.calculateSignature(X509Util.java:248)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.android.org.bouncycastle.x509.X509V3CertificateGenerator.generate(X509V3CertificateGenerator.java:434)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.android.org.bouncycastle.x509.X509V3CertificateGenerator.generate(X509V3CertificateGenerator.java:412)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at android.security.AndroidKeyPairGenerator.generateKeyPair(AndroidKeyPairGenerator.java:133)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.security.KeyPairGenerator$KeyPairGeneratorImpl.generateKeyPair(KeyPairGenerator.java:276)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.myprog.MyKeystoreWrapper.createKey(SourceFile:147)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: - locked <0x2b1e5aea> (a java.lang.Object)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.myprog.MyKeystoreWrapper.createFirstInstallData(SourceFile:70)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.myprog.MyProgLib.getReInstallData(SourceFile:675)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.myprog.MyProgLib.sendTrackingWithEvent(SourceFile:1110)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.myprog.MyProgLib.access$600(SourceFile:72)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at com.myprog.MyProgLib$d.run(SourceFile:2253)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-13 18:28:39.700 9359-10032/com.hoo.app.dev E/art: at java.lang.Thread.run(Thread.java:818)
Line: com.myprog.MyKeystoreWrapper.createKey(SourceFile:147)
points to my method
Upvotes: 1
Views: 261
Reputation: 346
As mentioned in your research, unchecked exceptions do not need to explicitly declared. Unchecked exceptions occur when the compiler cannot ascertain that no runtime exception will occur.
As explained in this post: How to catch an Exception from a thread
I would think that the exception occurs in a separate thread and thus you are unable to catch it with your surrounding catch clause. The post with most up votes (Dan Cruz) utilises the following method which is exposed by the Thread class:
setUncaughtExceptionHandler(ThreadExceptionHandler)
Using the solution as suggested by the user Dan Cruz, I would think that you are able to catch said runtime exception..
Upvotes: 1
Reputation: 131326
java.security.ProviderException :
A runtime exception for Provider exceptions (such as misconfiguration errors or unrecoverable internal errors), which may be subclassed by Providers to throw specialized, provider-specific runtime errors.
It refers among other things : unrecoverable internal errors.
In this case, it means that catching the exception is useless because the error will stop the application.
You are probably in this case.
On devices with SDK version < 23 I get java.security.ProviderException in line generator.generateKeyPair(); and Application Crashes!
If this bug is not resolvable for devices with SDK version < 23, you should test the SDK version in the code and apply an alternative for this securing task.
Upvotes: 2