java - Finally block will be executed in case of OutOfMemoryError? -


consider below code,

public class test {      public void abc() {         try {             throw new outofmemoryerror();         } {             system.out.println("finally");         }     }      public static void main(string[] args) {         new test().abc();     }  } 

output :

exception in thread "main" java.lang.outofmemoryerror     @ test.abc(test.java:5)     @ test.main(test.java:12) 

so, finally block getting executed in case, above output not fixed each time.

  1. so question is, here throwing outofmemoryerror , finally block getting executed. true every case ?

  2. if yes finally block executed when outofmemoryerror thrown in reality, means memory area expansion performed while execution , enough memory not available ?

things interesting if try in block requiring additional memory:

public class {      public void abc() {         try {             throw new outofmemoryerror();         } {             byte[] b = new byte[1024*1024*1024];             system.out.println("finally");         }     }      public static void main(string[] args) {         new finally().abc();     } } 

now when execute code (java -xmx20m finally example), can end in situation available heap has been exhausted extent finally block cannot complete, execution cannot guaranteed. not recommend rely on blocks in case of outofmemoryerrors.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -