Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with multi-exception catch blocks #421

Closed
jpstotz opened this issue Jan 2, 2019 · 4 comments
Closed

Problem with multi-exception catch blocks #421

jpstotz opened this issue Jan 2, 2019 · 4 comments

Comments

@jpstotz
Copy link
Collaborator

jpstotz commented Jan 2, 2019

Jadx currently has problems with catch blocks that catch more than one exception like this one:

    @TargetApi(Build.VERSION_CODES.O)
    public void test() {
        try {
            System.out.println("Test");
        } catch (ProviderException | DateTimeException e) {
            throw new RuntimeException(e);
        }
    }

See the attached minimal example app (in the MainActivity class):
app-debug.apk.zip

Note: Even if Android Studio only allows such code if minSdk is 26 or higher or the method is annotated like my example I have seen Android apps with minSdk 15 that use such code without annotation (e.g. Whatsapp).

@jpstotz jpstotz changed the title Problem with Java 8 style catch blocks Problem with multi-exception catch blocks Jan 2, 2019
@skylot
Copy link
Owner

skylot commented Jan 6, 2019

@jpstotz can you suggest how to better restore such code?
Possible options:

  1. multi-exception catch block (like in your example), but it will not compile back for older minSdk
  2. with separate catch block and duplicated code, will compile but too verbose:
    public void test() {
        try {
            System.out.println("Test");
        } catch (ProviderException e) {
            throw new RuntimeException(e);
        } catch (DateTimeException e) {
            throw new RuntimeException(e);
        }
    }
  1. or add preference to set target for generated code and allow to switch beetween above options

@jpstotz
Copy link
Collaborator Author

jpstotz commented Jan 6, 2019

My personal preference would be to re-generate the multi-exception catch block as it is the original code. Additionally readability is much better especially if the catch block becomes larger or the number of catched exceptions is higher than two.

From what I have read on StackOverflow multi-exception catch block works on Android since API 19, I don't know why Android Studio limits it to 26+. That some people may run into issues when trying to re-compile the Jadx generate decompiled project is a draw-back caused by the original source, not by Jadx.

Anyway I am fine with any of those three options. Just implement the option that you prefer respectively requires the least effort.

@skylot
Copy link
Owner

skylot commented Jan 19, 2019

@jpstotz I added support for multi-exceptions in a catch block. Only simple cases for now.

@jpstotz
Copy link
Collaborator Author

jpstotz commented Jan 19, 2019

@skylot Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants