4

I've got the following situation:

try{
    // Do some things that can cause the exceptions
}
catch(SomeException ex){
    doSomething();
}
catch(SomeOtherException ex){
    doSomething();
}
catch(AndYetAnotherException ex){
    doSomething();
}
catch(Exception ex){
    // Do something else
}

In Java v7+ I could change this to:

try{
    // Do some things that can cause the exceptions
}
catch(SomeException | SomeOtherException | AndYetAnotherException ex){
    doSomething();
}
catch(Exception ex){
    // Do something else
}

Since Android doesn't support Java 7+ yet, I can't use the above. What are the risks of doing the following instead:

try{
    // Do some things that can cause the exceptions
}
catch(Exception ex){
    if(ex instanceof SomeException || ex instanceof SomeOtherException || ex instanceof AndYetAnotherException){
        doSomething();
    }
    else{
        // Do something else
    }
}

I don't have enough experience or knowledge of instanceof, so I don't know the risks. Are there unexpected results that might occur? Are there performance changes during run-time and/or during compilation? etc.

If there aren't any risks, high performance changes or unexpected results, then why not use the instanceof for a single catch? If there are however any risk whatsoever, I guess it's better to use multiple catches which is supported better by both Android/Java itself and the compilation behind the scenes.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
  • You can use Java 7 to build Android apps. Java *8* is not presently supported AFAIK, unless you tell it to emit backwards-compatible bytecode. – CommonsWare Jul 04 '14 at 12:33
  • @CommonsWare I use the latest version of Eclipse and the Android SDK, but it gave me an error when I used `catch(SomeException | SomeOtherException ex)`. So if Java 7+ is supported by Android, how do I enable this in Eclipse? I've downloaded both Eclipse and the Android SDK about 2 months ago, so is it updated in the last 2 months and I just hadn't noticed this? – Kevin Cruijssen Jul 04 '14 at 12:39
  • 1
    Window > Preferences > Java > Compiler is where you tell Eclipse what syntax you want to support. "so is it updated in the last 2 months and I just hadn't noticed this?" -- Google just hasn't bothered to update the docs. On the `adt-dev` Google Group, they indicated that Java 7 is supported. – CommonsWare Jul 04 '14 at 12:42
  • 2
    If you are using Java 6, then definitely go with multiple catch, else you might catch exceptions, that you don't want to. Also if people should use instanceof then multiple catch wouldn't even exist. – Sipka Jul 04 '14 at 12:46
  • @CommonsWare Ah thanks. It's currently sitting on v1.6, but v1.7 is available as option in the dropdown. My work PC where I'm currently working on doesn't have the 1.7 JDK yet though, so I'll install it right away. Thanks for the answer. If you want you can make an answer I can accept, or should I make one myself crediting you? Your call. – Kevin Cruijssen Jul 04 '14 at 12:47

1 Answers1

7

You can update Eclipse to use Java 7 syntax checking, via Window > Preferences > Java > Compiler from the main menu (replacing "Windows" with the Apple menu on OS X IIRC).

However, note that you need to have API Level 19+ as your build SDK (Project > Properties > Android from the main menu). If it is lower, Eclipse/ADT will be unhappy, expressing that unhappiness in the form of lots of red pixels.

But, if you change the compiler setting and have your build SDK set to 19+, this does enable Java 7 syntax, including your SomeException | SomeOtherException | AndYetAnotherException syntax.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Is multicatch just syntactic sugar? Or does it require JVM support? – nmr Jan 26 '15 at 00:54
  • @nmr: Beats me, sorry. – CommonsWare Jan 26 '15 at 00:57
  • Wasn't able to find an easy answer, but I did test compatibility with a 2.2 AVD and multicatch works fine there. (Which is good enough for me.) http://stackoverflow.com/questions/17168184/java-7-language-backwards-compatibility seems to indicate it requires new JVM opcodes, but maybe that's handled in dx or something. Ahh, and now I've found http://stackoverflow.com/questions/7153989/java-7-language-features-with-android when I realized the magic non-polluted search term was "dalvik multicatch" ;) – nmr Jan 26 '15 at 01:13
  • 1
    @CommonsWare I am still a bit confused about the verdict, I want to use it on android 4.0+, can I use multicatch and try-with-resource or not? – dirtydexter Jun 15 '15 at 07:16
  • @dirtydexter: I know that there is a recipe for using Java 7 features like this on Android Studio, though I do not know the details off the top of my head. – CommonsWare Jun 15 '15 at 10:54
  • @dirtydexter: I did'n check but https://gyulajuhasz.com/blog/dangers-of-multi-catch-in-android/ states that ReflectiveOperationException breaks Android applications on API 18 and lower – rupashka Sep 05 '17 at 09:49