0

Android Studio 3.0 catch code path is always executed even if no exception thrown inside try. So in this code BOTH break points are hit and it returns false!!!

This only happens in Android Studio 3.0, works as excepted in intelliJ.

enter image description here

Akram Hassan
  • 191
  • 9
  • That means exception is occuring at `dateFrom.after(dateTo)` just see what is the exception and paste it – Manohar Feb 27 '18 at 05:13
  • you mean you hit the second breakpoint in debugger? if you inspect the exception what does it say? can you do e.printStacktrace(); and show that here as well? – Andrew Butenko Feb 27 '18 at 05:13
  • and dont post as image post code and exception as text – Manohar Feb 27 '18 at 05:13
  • The debugger is unreliable when you have multiple `return` statements in a single method. If you really want to use it there, refactor the method to have only one `return`. I would also mention that your comparison doesn't seem to match what you're checking there. That is, it's returning true if the `from` date is _after_ the `to` date. Is that what you intended? – Mike M. Feb 27 '18 at 05:26
  • To elaborate on what @Redman said, put `boolean ret = dateFrom.after(dateTo);` `return ret;` with breakpoint on former line. Then debugger should work as expected. – AnOccasionalCashew Feb 27 '18 at 05:28
  • @Vishal Chhodwani isn't that exactly what i suggested to Akram? – Andrew Butenko Feb 27 '18 at 05:34
  • 1
    @Mike M I have never seen debugger issues with multiple return statements. – Andrew Butenko Feb 27 '18 at 05:34
  • 2
    @AndrewButenko Well, you're seeing one here. :-) In all seriousness, though, it's an Android-specific thing. IIRC, it's because multiple returns are actually compiled into a single return, for some odd reason, so the debugger isn't sure which line is executing for the return. I'm looking for the duplicate for this now. It's answered by a Google engineer. – Mike M. Feb 27 '18 at 06:07
  • @AndrewButenko Sorry, got distracted. The duplicate I mentioned is linked above. It was as I recalled; multiple returns compiled into one. – Mike M. Feb 27 '18 at 07:23
  • Akram, I tested your code to confirm, and observed the exact behavior you describe. It's just the debugger. The actual returned value is correct. If it's not what you're expecting, then I believe it's because you've got your comparison backwards, as I mentioned above. – Mike M. Feb 27 '18 at 07:23

0 Answers0