OutOfMemoryError exception
KW.java
class KW { public static void main(String args[]) { int size=Integer.MAX_VALUE; System.out.println(size); String[] s=new String[size]; } }
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW 2147483647Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit at KW.main(KW.java:7) kodingwindow@kw:~$
Java program to handle the OutOfMemoryError exception
KW.java
class KW { public static void main(String args[]) { int size=Integer.MAX_VALUE; try { String[] s=new String[size]; } catch(OutOfMemoryError e) { System.out.println("Array overflow"); System.out.println(e.toString()); } } }
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Array overflow java.lang.OutOfMemoryError: Requested array size exceeds VM limit kodingwindow@kw:~$
Comments and Reactions
Advertisement