-1

I want to ask which catch block will be called first, in case of statement something like

try {
  ...some statement...
} catch (Exception ex) {
  System.out.println("out of memory exception");
} catch (OutOfMemoryError error) {
  System.out.println("out of memory error");
}

here statement causes OutOfMemoryError. I have checked Order of catching exceptions in Java

This is something different, You can't predict that there would be a RunTimeException occurs if code runs seamlessly.

Community
  • 1
  • 1
  • 2
    Exceptions and errors are orthogonal (they have a common supertype, but one is not the supertype of the other). An exception is caught if an exception is thrown, an error is caught if an error is thrown. – Andy Turner Feb 13 '17 at 09:19
  • In case of low memory OutOfMemoryError catch block will be executed with app crash – Pratik Popat Feb 13 '17 at 09:21
  • @EJP your duplicate concerns only Exceptions, this question is slightly different (Exception vs. Error). (Yes probably it's still a duplicate somewhere else ;) but not this one) – Jeroen van Dijk-Jun Feb 13 '17 at 09:22
  • @JeroenvanDijk-Jun The issue is exactly the same whichever pair of `Throwables` you choose. If there is an inheritance relationship, the compiler will enforce it, otherwise there is no problem to solve. – user207421 Feb 13 '17 at 09:26
  • @EJP well... yes... but... it's not that clealry answered for throwable siblings, but only for exception siblings. Less experienced people could find this one useful for understanding exception vs. error. – Jeroen van Dijk-Jun Feb 13 '17 at 09:30
  • @JeroenvanDijk-Jun It doesn't make any difference what the siblings have in common. That's why it's a duplicate. General principles apply. Otherwise we would have to have hundreds of answers to essentially the same question. – user207421 Feb 13 '17 at 09:38
  • @PratikPopat In the case of `OutOfMemoryError`, it will be caught by the corresponding `catch` block *instead* of an application crash, unless the error recurs. – user207421 Feb 13 '17 at 09:43
  • @EJP I tried with OutOfMemory catch block but still app crashing – Pratik Popat Feb 13 '17 at 09:51
  • @PratikPopat Don't post code in comments. You can see for yourself that it is completely illegible. I have no idea what you're trying to tell me, if anything. – user207421 Feb 13 '17 at 09:55
  • @EJB This is not duplicate as you suggest. – kishorsinghgour Feb 15 '17 at 10:57
  • @AndyTurner Thanks, you are right. – kishorsinghgour Feb 15 '17 at 11:08

2 Answers2

4

Pls keep in mind the Object hierarchy:

  • Java.lang.Throwable
    • Java.lang.Exception
    • Java.lang.Error

If something goes wrong, it's either an ERROR, or an EXCEPTION. There is no first here, as these classes are siblings, not parent/child.

Jeroen van Dijk-Jun
  • 1,028
  • 10
  • 22
0

java.lang.Exception and java.lang.Error(OutOfMemory is an Error) are not related to each other exception for their common parent Throwable. So there's no first or second here.

OutOfMemory happens when your jvm is dying(heap full) which is rare, so just do cleanup work inside that.

Yank Leo
  • 452
  • 5
  • 19