diff --git a/app/src/main/java/com/hippo/ehviewer/EhDB.java b/app/src/main/java/com/hippo/ehviewer/EhDB.java index f9ea510d2..08bdfe022 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhDB.java +++ b/app/src/main/java/com/hippo/ehviewer/EhDB.java @@ -43,6 +43,7 @@ import com.hippo.ehviewer.dao.LocalFavoritesDao; import com.hippo.ehviewer.dao.QuickSearch; import com.hippo.ehviewer.dao.QuickSearchDao; import com.hippo.ehviewer.download.DownloadManager; +import com.hippo.util.ExceptionUtils; import com.hippo.util.SqlUtils; import com.hippo.yorozuya.FileUtils; import com.hippo.yorozuya.IOUtils; @@ -156,7 +157,8 @@ public class EhDB { SQLiteDatabase oldDB; try { oldDB = oldDBHelper.getReadableDatabase(); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); return; } @@ -178,7 +180,8 @@ public class EhDB { try { // In 0.6.x version, NaN is stored gi.rating = cursor.getFloat(7); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); gi.rating = -1.0f; } @@ -189,7 +192,8 @@ public class EhDB { } cursor.close(); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } @@ -219,7 +223,8 @@ public class EhDB { } cursor.close(); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } @@ -255,7 +260,8 @@ public class EhDB { } cursor.close(); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } @@ -296,7 +302,8 @@ public class EhDB { } cursor.close(); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } @@ -325,13 +332,15 @@ public class EhDB { } cursor.close(); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } try { oldDBHelper.close(); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } } @@ -726,7 +735,8 @@ public class EhDB { } return null; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore return context.getString(R.string.cant_read_the_file); } diff --git a/app/src/main/java/com/hippo/ehviewer/Settings.java b/app/src/main/java/com/hippo/ehviewer/Settings.java index d62f9145b..ccb94f825 100755 --- a/app/src/main/java/com/hippo/ehviewer/Settings.java +++ b/app/src/main/java/com/hippo/ehviewer/Settings.java @@ -30,6 +30,7 @@ import com.hippo.ehviewer.client.data.FavListUrlBuilder; import com.hippo.ehviewer.ui.CommonOperations; import com.hippo.glgallery.GalleryView; import com.hippo.unifile.UniFile; +import com.hippo.util.ExceptionUtils; import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.FileUtils; import com.hippo.yorozuya.MathUtils; @@ -633,7 +634,8 @@ public class Settings { builder.encodedQuery(getString(KEY_DOWNLOAD_SAVE_QUERY, null)); builder.encodedFragment(getString(KEY_DOWNLOAD_SAVE_FRAGMENT, null)); dir = UniFile.fromUri(sContext, builder.build()); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } return dir != null ? dir : UniFile.fromFile(AppConfig.getDefaultDownloadDir()); diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java index 1ae3385be..cd6c506ed 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -18,13 +18,12 @@ package com.hippo.ehviewer.client; import android.content.Context; import android.os.AsyncTask; - import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.exception.CancelledException; +import com.hippo.util.ExceptionUtils; import com.hippo.yorozuya.SimpleHandler; import com.hippo.yorozuya.thread.PriorityThreadFactory; - import java.io.File; import java.util.List; import java.util.concurrent.BlockingQueue; @@ -34,7 +33,6 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; - import okhttp3.Call; import okhttp3.OkHttpClient; @@ -193,7 +191,8 @@ public class EhClient { default: return new IllegalStateException("Can't detect method " + mMethod); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); return e; } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java index 40d1d531f..680899475 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -47,6 +47,7 @@ import com.hippo.ehviewer.client.parser.TorrentParser; import com.hippo.ehviewer.client.parser.VoteCommentParser; import com.hippo.ehviewer.client.parser.WhatsHotParser; import com.hippo.network.StatusCodeException; +import com.hippo.util.ExceptionUtils; import com.hippo.yorozuya.AssertUtils; import java.io.File; import java.util.ArrayList; @@ -88,7 +89,7 @@ public class EhEngine { } private static void doThrowException(Call call, int code, @Nullable Headers headers, - @Nullable String body, Exception e) throws Exception { + @Nullable String body, Throwable e) throws Throwable { if (call.isCanceled()) { throw new CancelledException(); } @@ -121,7 +122,7 @@ public class EhEngine { } private static void throwException(Call call, int code, @Nullable Headers headers, - @Nullable String body, Exception e) throws Exception { + @Nullable String body, Throwable e) throws Throwable { try { doThrowException(call, code, headers, body, e); } catch (Throwable error) { @@ -131,7 +132,7 @@ public class EhEngine { } public static String signIn(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - String username, String password, String recaptchaChallenge, String recaptchaResponse) throws Exception { + String username, String password, String recaptchaChallenge, String recaptchaResponse) throws Throwable { FormBody.Builder builder = new FormBody.Builder() .add("UserName", username) .add("PassWord", password) @@ -163,14 +164,15 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); return SignInParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } } public static GalleryListParser.Result getGalleryList(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - String url) throws Exception { + String url) throws Throwable { Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); Call call = okHttpClient.newCall(request); @@ -190,7 +192,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); result = GalleryListParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -230,7 +233,7 @@ public class EhEngine { // At least, GalleryInfo contain valid gid and token public static List fillGalleryListByApi(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - List galleryInfoList) throws Exception { + List galleryInfoList) throws Throwable { // We can only request 25 items one time at most final int MAX_REQUEST_SIZE = 25; List requestItems = new ArrayList<>(MAX_REQUEST_SIZE); @@ -245,7 +248,7 @@ public class EhEngine { } private static void doFillGalleryListByApi(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - List galleryInfoList) throws Exception { + List galleryInfoList) throws Throwable { JSONObject json = new JSONObject(); json.put("method", "gdata"); JSONArray ja = new JSONArray(); @@ -279,14 +282,15 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); GalleryApiParser.parse(body, galleryInfoList); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } } public static GalleryDetail getGalleryDetail(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - String url) throws Exception { + String url) throws Throwable { Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); Call call = okHttpClient.newCall(request); @@ -305,7 +309,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); return GalleryDetailParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -313,7 +318,7 @@ public class EhEngine { public static Pair getPreviewSet( - @Nullable EhClient.Task task, OkHttpClient okHttpClient, String url) throws Exception { + @Nullable EhClient.Task task, OkHttpClient okHttpClient, String url) throws Throwable { Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); Call call = okHttpClient.newCall(request); @@ -333,7 +338,8 @@ public class EhEngine { body = response.body().string(); return Pair.create(GalleryDetailParser.parsePreviewSet(body), GalleryDetailParser.parsePreviewPages(body)); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -341,7 +347,7 @@ public class EhEngine { public static RateGalleryParser.Result rateGallery(@Nullable EhClient.Task task, OkHttpClient okHttpClient, long apiUid, String apiKey, long gid, - String token, float rating) throws Exception { + String token, float rating) throws Throwable { final JSONObject json = new JSONObject(); json.put("method", "rategallery"); json.put("apiuid", apiUid); @@ -371,14 +377,15 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); return RateGalleryParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } } public static GalleryComment[] commentGallery(@Nullable EhClient.Task task, - OkHttpClient okHttpClient, String url, String comment) throws Exception { + OkHttpClient okHttpClient, String url, String comment) throws Throwable { FormBody.Builder builder = new FormBody.Builder() .add("commenttext_new", comment); Log.d(TAG, url); @@ -408,14 +415,15 @@ public class EhEngine { } return GalleryDetailParser.parseComments(document); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } } public static String getGalleryToken(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - long gid, String gtoken, int page) throws Exception { + long gid, String gtoken, int page) throws Throwable { JSONObject json = new JSONObject() .put("method", "gtoken") .put("pagelist", new JSONArray().put( @@ -442,14 +450,15 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); return GalleryTokenApiParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } } public static FavoritesParser.Result getFavorites(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - String url, boolean callApi) throws Exception { + String url, boolean callApi) throws Throwable { Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); Call call = okHttpClient.newCall(request); @@ -469,7 +478,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); result = FavoritesParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -492,7 +502,7 @@ public class EhEngine { * @param note max 250 characters */ public static Void addFavorites(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - long gid, String token, int dstCat, String note) throws Exception { + long gid, String token, int dstCat, String note) throws Throwable { String catStr; if (dstCat == -1) { catStr = "favdel"; @@ -528,7 +538,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); throwException(call, code, headers, body, null); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -537,7 +548,7 @@ public class EhEngine { } public static Void addFavoritesRange(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - long[] gidArray, String[] tokenArray, int dstCat) throws Exception { + long[] gidArray, String[] tokenArray, int dstCat) throws Throwable { AssertUtils.assertEquals(gidArray.length, tokenArray.length); for (int i = 0, n = gidArray.length; i < n; i++) { addFavorites(task, okHttpClient, gidArray[i], tokenArray[i], dstCat, null); @@ -546,7 +557,7 @@ public class EhEngine { } public static FavoritesParser.Result modifyFavorites(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - String url, long[] gidArray, int dstCat, boolean callApi) throws Exception { + String url, long[] gidArray, int dstCat, boolean callApi) throws Throwable { String catStr; if (dstCat == -1) { catStr = "delete"; @@ -582,7 +593,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); result = FavoritesParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -600,7 +612,7 @@ public class EhEngine { } public static Pair[] getTorrentList(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - String url) throws Exception { + String url) throws Throwable { Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); Call call = okHttpClient.newCall(request); @@ -620,7 +632,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); result = TorrentParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -629,7 +642,7 @@ public class EhEngine { } public static Pair[]> getArchiveList(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - String url) throws Exception { + String url) throws Throwable { Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); Call call = okHttpClient.newCall(request); @@ -649,7 +662,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); result = ArchiveParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -658,7 +672,7 @@ public class EhEngine { } public static Void downloadArchive(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - long gid, String token, String or, String res) throws Exception { + long gid, String token, String or, String res) throws Throwable { if (or == null || or.length() == 0) { throw new EhException("Invalid form param or: " + or); } @@ -688,7 +702,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); throwException(call, code, headers, body, null); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -702,7 +717,7 @@ public class EhEngine { } public static List getWhatsHot(@Nullable EhClient.Task task, - OkHttpClient okHttpClient) throws Exception { + OkHttpClient okHttpClient) throws Throwable { String url = EhUrl.HOST_E; Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); @@ -723,7 +738,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); list = WhatsHotParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -741,7 +757,7 @@ public class EhEngine { } private static ProfileParser.Result getProfileInternal(@Nullable EhClient.Task task, - OkHttpClient okHttpClient, String url) throws Exception { + OkHttpClient okHttpClient, String url) throws Throwable { Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); Call call = okHttpClient.newCall(request); @@ -760,14 +776,15 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); return ProfileParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } } public static ProfileParser.Result getProfile(@Nullable EhClient.Task task, - OkHttpClient okHttpClient) throws Exception { + OkHttpClient okHttpClient) throws Throwable { String url = EhUrl.URL_FORUMS; Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); @@ -787,14 +804,15 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); return getProfileInternal(task, okHttpClient, ForumsParser.parse(body)); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } } public static VoteCommentParser.Result voteComment(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - long apiUid, String apiKey, long gid, String token, long commentId, int commentVote) throws Exception { + long apiUid, String apiKey, long gid, String token, long commentId, int commentVote) throws Throwable { final JSONObject json = new JSONObject(); json.put("method", "votecomment"); json.put("apiuid", apiUid); @@ -825,7 +843,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); return VoteCommentParser.parse(body, commentVote); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -835,7 +854,7 @@ public class EhEngine { * @param image Must be jpeg */ public static GalleryListParser.Result imageSearch(@Nullable EhClient.Task task, OkHttpClient okHttpClient, - File image, boolean uss, boolean osc, boolean se) throws Exception { + File image, boolean uss, boolean osc, boolean se) throws Throwable { MultipartBody.Builder builder = new MultipartBody.Builder(); builder.setType(MultipartBody.FORM); builder.addPart( @@ -889,7 +908,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); result = GalleryListParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } @@ -928,7 +948,7 @@ public class EhEngine { } public static GalleryPageParser.Result getGalleryPage(@Nullable EhClient.Task task, - OkHttpClient okHttpClient, String url) throws Exception { + OkHttpClient okHttpClient, String url) throws Throwable { Log.d(TAG, url); Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); Call call = okHttpClient.newCall(request); @@ -947,7 +967,8 @@ public class EhEngine { headers = response.headers(); body = response.body().string(); return GalleryPageParser.parse(body); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throwException(call, code, headers, body, e); throw e; } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java index e63f45c37..66c321130 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.java @@ -21,6 +21,7 @@ import com.hippo.ehviewer.R; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.exception.EhException; import com.hippo.ehviewer.client.exception.ParseException; +import com.hippo.util.ExceptionUtils; import com.hippo.util.JsoupUtils; import com.hippo.yorozuya.AssertUtils; import java.util.List; @@ -58,7 +59,8 @@ public class FavoritesParser { countArray[i] = ParserUtils.parseInt(fp.child(0).text()); catArray[i] = ParserUtils.trim(fp.child(2).text()); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); e.printStackTrace(); throw new ParseException("Parse favorites error", body); } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ForumsParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/ForumsParser.java index 3f77b2180..2e591d362 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ForumsParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ForumsParser.java @@ -18,7 +18,7 @@ package com.hippo.ehviewer.client.parser; import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.exception.ParseException; - +import com.hippo.util.ExceptionUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -31,7 +31,8 @@ public class ForumsParser { Element userlinks = d.getElementById("userlinks"); Element child = userlinks.child(0).child(0).child(0); return child.attr("href"); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throw new ParseException("Parse forums error", body); } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.java index f7d195e26..2d4078845 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.java @@ -19,7 +19,6 @@ package com.hippo.ehviewer.client.parser; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.text.TextUtils; - import com.hippo.ehviewer.Settings; import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.EhUtils; @@ -33,15 +32,10 @@ import com.hippo.ehviewer.client.exception.EhException; import com.hippo.ehviewer.client.exception.OffensiveException; import com.hippo.ehviewer.client.exception.ParseException; import com.hippo.ehviewer.client.exception.PiningException; +import com.hippo.util.ExceptionUtils; import com.hippo.util.JsoupUtils; import com.hippo.yorozuya.NumberUtils; import com.hippo.yorozuya.StringUtils; - -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; - import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -52,6 +46,10 @@ import java.util.Locale; import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; public class GalleryDetailParser { @@ -142,7 +140,8 @@ public class GalleryDetailParser { Element gd1 = gm.getElementById("gd1"); try { gd.thumb = parseCoverStyle(StringUtils.trim(gd1.child(0).attr("style"))); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); gd.thumb = ""; } @@ -168,7 +167,8 @@ public class GalleryDetailParser { String href = gdc.child(0).attr("href"); String category = href.substring(href.lastIndexOf('/') + 1); gd.category = EhUtils.getCategory(category); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); gd.category = EhUtils.UNKNOWN; } @@ -193,7 +193,8 @@ public class GalleryDetailParser { for (int i = 0, n = es.size(); i < n; i++) { parseDetailInfo(gd, es.get(i), body); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } @@ -235,7 +236,8 @@ public class GalleryDetailParser { gd.favoriteName = StringUtils.trim(gdf.text()); } } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throw new ParseException("Can't parse gallery detail", body); } } @@ -317,7 +319,8 @@ public class GalleryDetailParser { } return group.size() > 0 ? group : null; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); e.printStackTrace(); return null; } @@ -340,7 +343,8 @@ public class GalleryDetailParser { } } return list.toArray(new GalleryTagGroup[list.size()]); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); e.printStackTrace(); return EMPTY_GALLERY_TAG_GROUP_ARRAY; } @@ -412,7 +416,8 @@ public class GalleryDetailParser { // comment comment.comment = JsoupUtils.getElementByClass(element, "c6").html(); return comment; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); e.printStackTrace(); return null; } @@ -435,7 +440,8 @@ public class GalleryDetailParser { } } return list.toArray(new GalleryComment[list.size()]); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); e.printStackTrace(); return EMPTY_GALLERY_COMMENT_ARRAY; } @@ -474,7 +480,8 @@ public class GalleryDetailParser { try { Elements elements = document.getElementsByClass("ptt").first().child(0).child(0).children(); return Integer.parseInt(elements.get(elements.size() - 2).text()); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); e.printStackTrace(); throw new ParseException("Can't parse preview pages", body); } @@ -549,7 +556,8 @@ public class GalleryDetailParser { largePreviewSet.addItem(index, imageUrl, pageUrl); } return largePreviewSet; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); e.printStackTrace(); throw new ParseException("Can't parse large preview", body); } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java index cade76215..72b96d4c8 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.java @@ -19,24 +19,22 @@ package com.hippo.ehviewer.client.parser; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; - import com.hippo.ehviewer.client.EhUtils; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.exception.ParseException; +import com.hippo.util.ExceptionUtils; import com.hippo.util.JsoupUtils; import com.hippo.yorozuya.NumberUtils; import com.hippo.yorozuya.StringUtils; - -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; - import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; public class GalleryListParser { @@ -54,7 +52,8 @@ public class GalleryListParser { try { Elements es = d.getElementsByClass("ptt").first().child(0).child(0).children(); return Integer.parseInt(es.get(es.size() - 2).text().trim()); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throw new ParseException("Can't parse gallery list pages", body); } } @@ -265,7 +264,8 @@ public class GalleryListParser { } } result.galleryInfoList = list; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throw new ParseException("Can't parse gallery list", body); } @@ -281,7 +281,8 @@ public class GalleryListParser { } } result.galleryInfoList = list; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throw new ParseException("Can't parse gallery list", body); } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryTokenApiParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryTokenApiParser.java index a0589825f..774e988bf 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryTokenApiParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryTokenApiParser.java @@ -17,7 +17,7 @@ package com.hippo.ehviewer.client.parser; import com.hippo.ehviewer.client.exception.EhException; - +import com.hippo.util.ExceptionUtils; import org.json.JSONObject; public class GalleryTokenApiParser { @@ -36,7 +36,8 @@ public class GalleryTokenApiParser { JSONObject jo = new JSONObject(body).getJSONArray("tokenlist").getJSONObject(0); try { return jo.getString("token"); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throw new EhException(jo.getString("error")); } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ProfileParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/ProfileParser.java index df093bf83..37dc5661b 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ProfileParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ProfileParser.java @@ -18,10 +18,9 @@ package com.hippo.ehviewer.client.parser; import android.text.TextUtils; import android.util.Log; - import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.exception.ParseException; - +import com.hippo.util.ExceptionUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -43,11 +42,13 @@ public class ProfileParser { } else if (!result.avatar.startsWith("http")) { result.avatar = EhUrl.URL_FORUMS + result.avatar; } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); Log.i(TAG, "No avatar"); } return result; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throw new ParseException("Parse forums error", body); } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java index 2289053d0..dbc27717b 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/WhatsHotParser.java @@ -19,16 +19,15 @@ package com.hippo.ehviewer.client.parser; import com.hippo.ehviewer.client.EhUtils; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.exception.ParseException; +import com.hippo.util.ExceptionUtils; import com.hippo.util.JsoupUtils; - +import java.util.ArrayList; +import java.util.List; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import java.util.ArrayList; -import java.util.List; - public class WhatsHotParser { @SuppressWarnings("ConstantConditions") @@ -54,7 +53,8 @@ public class WhatsHotParser { galleryInfoList.add(galleryInfo); } return galleryInfoList; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); throw new ParseException("Parse whats hot error", body); } } diff --git a/app/src/main/java/com/hippo/ehviewer/preference/RestoreDownloadPreference.java b/app/src/main/java/com/hippo/ehviewer/preference/RestoreDownloadPreference.java index be5879127..23975e6ef 100644 --- a/app/src/main/java/com/hippo/ehviewer/preference/RestoreDownloadPreference.java +++ b/app/src/main/java/com/hippo/ehviewer/preference/RestoreDownloadPreference.java @@ -23,7 +23,6 @@ import android.preference.Preference; import android.support.annotation.NonNull; import android.util.AttributeSet; import android.widget.Toast; - import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.EhDB; import com.hippo.ehviewer.R; @@ -34,14 +33,13 @@ import com.hippo.ehviewer.download.DownloadManager; import com.hippo.ehviewer.spider.SpiderInfo; import com.hippo.ehviewer.spider.SpiderQueen; import com.hippo.unifile.UniFile; +import com.hippo.util.ExceptionUtils; import com.hippo.yorozuya.IOUtils; - import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; - import okhttp3.OkHttpClient; public class RestoreDownloadPreference extends TaskPreference { @@ -133,7 +131,8 @@ public class RestoreDownloadPreference extends TaskPreference { try { return EhEngine.fillGalleryListByApi(null, mHttpClient, new ArrayList(restoreItemList)); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); e.printStackTrace(); return null; } diff --git a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java index 9a9be1ad9..2866938f7 100644 --- a/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java +++ b/app/src/main/java/com/hippo/ehviewer/spider/SpiderQueen.java @@ -749,7 +749,8 @@ public final class SpiderQueen implements Runnable { spiderInfo.pTokenMap = new SparseArray<>(spiderInfo.pages); readPreviews(body, 0, spiderInfo); return spiderInfo; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); return null; } } @@ -791,7 +792,8 @@ public final class SpiderQueen implements Runnable { pToken = spiderInfo.pTokenMap.get(index); } return pToken; - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); return null; } } @@ -803,7 +805,8 @@ public final class SpiderQueen implements Runnable { UniFile file = downloadDir.createFile(SPIDER_INFO_FILENAME); try { spiderInfo.write(file.openOutputStream()); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); // Ignore } } @@ -1034,7 +1037,7 @@ public final class SpiderQueen implements Runnable { return pageUrl; } - private GalleryPageParser.Result getImageUrl(int index, String pageUrl) throws Exception { + private GalleryPageParser.Result getImageUrl(int index, String pageUrl) throws Throwable { GalleryPageParser.Result result = EhEngine.getGalleryPage(null, mHttpClient, pageUrl); if (StringUtils.endsWith(result.imageUrl, URL_509_SUFFIX_ARRAY)) { // Get 509 @@ -1071,7 +1074,8 @@ public final class SpiderQueen implements Runnable { result = getImageUrl(index, pageUrl); } catch (Image509Exception e) { error = GetText.getString(R.string.error_509); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); error = ExceptionUtils.getReadableString(e); } if (result == null) { diff --git a/app/src/main/java/com/hippo/util/ExceptionUtils.java b/app/src/main/java/com/hippo/util/ExceptionUtils.java index a437ff9b3..dda12d604 100644 --- a/app/src/main/java/com/hippo/util/ExceptionUtils.java +++ b/app/src/main/java/com/hippo/util/ExceptionUtils.java @@ -35,7 +35,7 @@ public final class ExceptionUtils { private static final String TAG = ExceptionUtils.class.getSimpleName(); @NonNull - public static String getReadableString(@NonNull Exception e) { + public static String getReadableString(@NonNull Throwable e) { e.printStackTrace(); if (e instanceof MalformedURLException) { return GetText.getString(R.string.error_invalid_url); @@ -62,4 +62,15 @@ public final class ExceptionUtils { return GetText.getString(R.string.error_unknown); } } + + public static void throwIfFatal(@NonNull Throwable t) { + // values here derived from https://github.com/ReactiveX/RxJava/issues/748#issuecomment-32471495 + if (t instanceof VirtualMachineError) { + throw (VirtualMachineError) t; + } else if (t instanceof ThreadDeath) { + throw (ThreadDeath) t; + } else if (t instanceof LinkageError) { + throw (LinkageError) t; + } + } } diff --git a/app/src/main/java/com/hippo/util/PackageUtils.java b/app/src/main/java/com/hippo/util/PackageUtils.java index 296a136dc..364b6849d 100644 --- a/app/src/main/java/com/hippo/util/PackageUtils.java +++ b/app/src/main/java/com/hippo/util/PackageUtils.java @@ -40,7 +40,8 @@ public final class PackageUtils { } else { Log.e(TAG, "Can't find signature in package " + packageName); } - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); Log.e(TAG, "Can't find package " + packageName, e); } return null; diff --git a/app/src/main/java/com/hippo/widget/HackyTextInputEditText.java b/app/src/main/java/com/hippo/widget/HackyTextInputEditText.java index c053c9aee..0d6b768ae 100644 --- a/app/src/main/java/com/hippo/widget/HackyTextInputEditText.java +++ b/app/src/main/java/com/hippo/widget/HackyTextInputEditText.java @@ -21,6 +21,7 @@ import android.os.Build; import android.support.design.widget.TextInputEditText; import android.util.AttributeSet; import android.widget.TextView; +import com.hippo.util.ExceptionUtils; import java.lang.reflect.Field; // Avoid crash on some Meizu devices @@ -40,7 +41,8 @@ public class HackyTextInputEditText extends TextInputEditText { if (isMeizu && isPreP) { try { return getSuperHint(); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); return super.getHint(); } } else { diff --git a/app/src/main/java/com/hippo/widget/SafeCoordinatorLayout.java b/app/src/main/java/com/hippo/widget/SafeCoordinatorLayout.java index 37feb9cb2..9c2944e71 100644 --- a/app/src/main/java/com/hippo/widget/SafeCoordinatorLayout.java +++ b/app/src/main/java/com/hippo/widget/SafeCoordinatorLayout.java @@ -20,6 +20,7 @@ import android.content.Context; import android.support.design.widget.CoordinatorLayout; import android.util.AttributeSet; import android.view.MotionEvent; +import com.hippo.util.ExceptionUtils; public class SafeCoordinatorLayout extends CoordinatorLayout { @@ -39,7 +40,8 @@ public class SafeCoordinatorLayout extends CoordinatorLayout { public boolean onInterceptTouchEvent(MotionEvent ev) { try { return super.onInterceptTouchEvent(ev); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); return false; } } @@ -48,7 +50,8 @@ public class SafeCoordinatorLayout extends CoordinatorLayout { public boolean onTouchEvent(MotionEvent ev) { try { return super.onTouchEvent(ev); - } catch (Exception e) { + } catch (Throwable e) { + ExceptionUtils.throwIfFatal(e); return false; } }