即使尝试catch也会获取RuntimeException

时间:2017-04-16 16:38:26

标签: java android try-catch runtimeexception

我有代码段:

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

在具有SDK版本<的设备上23我在第java.security.ProviderExceptiongenerator.generateKeyPair();和应用程序崩溃了!

我的问题是:为什么应用程序崩溃,如果我尝试/ catch?

从这个回答:Why we don't have to add try-catch to a RuntimeException?

  

那是因为它是一个未经检查的例外。它不需要   明确宣布或捕获。另请参阅Sun教程   主题。

但我认为不是我的情况

我的堆栈跟踪:

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)

行:com.myprog.MyKeystoreWrapper.createKey(SourceFile:147)指向我的方法

2 个答案:

答案 0 :(得分:2)

  

java.security.ProviderException:

     

Provider异常的运行时异常(例如配置错误   错误或不可恢复的内部错误),可能是子类   提供者抛出特定于提供者的特定运行时错误。

除其他外:不可恢复的内部错误 在这种情况下,这意味着捕获异常是无用的,因为错误将停止应用程序 你可能就是这种情况。

  

在具有SDK版本&lt;的设备上23我得到了java.security.ProviderException   在行generator.generateKeyPair();和应用程序崩溃!

如果具有SDK版本&lt;的设备无法解决此错误? 23,您应该在代码中测试SDK版本并为此安全任务应用替代方案。

答案 1 :(得分:1)

正如您的研究中所提到的,未经检查的异常不需要明确声明。当编译器无法确定不会发生运行时异常时,会发生未经检查的异常。

正如本文所述:How to catch an Exception from a thread

我认为异常发生在一个单独的线程中,因此你无法用你周围的catch子句捕获它。投票最多的帖子(Dan Cruz)使用Thread类公开的以下方法:

setUncaughtExceptionHandler(ThreadExceptionHandler)

使用用户Dan Cruz建议的解决方案,我认为你能够捕获所说的运行时异常..