A Simple Example of creating Out Of Memory Error in a Java Program . It is an Error , so you can catch it also .
import java.util.*; public class OutOfMemoryTest { public static void main(String[] args) { try { LinkedList list = new LinkedList(); while(true) { list.add(new Object()); } } catch(Throwable e) { System.out.println("caught it "); } } }