How to catch this exception in android web browser?

Due to an error in Android 4.3, my application crashes when trying to load certain web pages in webview

The stack trace looks like this:

09-16 14:16:48.221: E/AndroidRuntime(22487): FATAL EXCEPTION: WebViewCoreThread 09-16 14:16:48.221: E/AndroidRuntime(22487): java.lang.StringIndexOutOfBoundsException: length=0; index=-1 09-16 14:16:48.221: E/AndroidRuntime(22487): at java.lang.AbstractStringBuilder.indexAndLength(AbstractStringBuilder.java:212) 09-16 14:16:48.221: E/AndroidRuntime(22487): at java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:206) 09-16 14:16:48.221: E/AndroidRuntime(22487): at java.lang.StringBuffer.charAt(StringBuffer.java:346) 09-16 14:16:48.221: E/AndroidRuntime(22487): at com.android.org.bouncycastle.asn1.x509.X509NameTokenizer.nextToken(X509NameTokenizer.java:78) 09-16 14:16:48.221: E/AndroidRuntime(22487): at com.android.org.bouncycastle.asn1.x509.X509Name.<init>(X509Name.java:719) 09-16 14:16:48.221: E/AndroidRuntime(22487): at com.android.org.bouncycastle.asn1.x509.X509Name.<init>(X509Name.java:655) 09-16 14:16:48.221: E/AndroidRuntime(22487): at com.android.org.bouncycastle.asn1.x509.X509Name.<init>(X509Name.java:593) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.net.http.SslCertificate$DName.<init>(SslCertificate.java:379) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.net.http.SslCertificate.<init>(SslCertificate.java:189) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.net.http.SslCertificate.<init>(SslCertificate.java:178) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.webkit.BrowserFrame.setCertificate(BrowserFrame.java:1206) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:113) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.os.Handler.dispatchMessage(Handler.java:99) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.os.Looper.loop(Looper.java:137) 09-16 14:16:48.221: E/AndroidRuntime(22487): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:814) 09-16 14:16:48.221: E/AndroidRuntime(22487): at java.lang.Thread.run(Thread.java:841) 

In my webview, I have onReceivedSslError and the onReceivedError methods are overridden, but none of them can catch this exception.

 try{ webview.postUrl(url, EncodingUtils.getBytes(data, "BASE64")); } catch(Exception e){ System.out.println("Caught the exception!"); } 

the surrounding call to postUrl using the try / catch block (as indicated above) also throws no exception.

Is there a way to catch this exception so that I can display a meaningful error message instead of allowing the application to crash?

+4
source share
2 answers

You may need to try creating a global exception handler. You do this by expanding the application and defining an uncaught exception handler in it. It will look something like this:

 public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, final Throwable ex) { // Custom code here to handle the error. } }); } } 

Just make sure you specify your own application class in the manifest.
Give it a shot. hope this helps.

+5
source

Regarding the question of why the exception could not be caught at the activity level, the reason is that the exception occurred in a new thread and not in the try block.

The setDefaultUncaughtExceptionHandler function sometimes does not work, because the default handler may be overwritten with later code.

And you cannot recover from an exception in UncaughtExceptionHandler. All you can do is register it and kill the process itself.

0
source

Source: https://habr.com/ru/post/1502366/


All Articles