Exception handling

If I write a program that will generate OutOfMemoryError, and catch it using Throwable then the line after catch does not execute, but if I use Error instead of Throwable then it gets executed. Why so?

import java.util.*;

class TestExcep

{

public static void main(String[] args)

{Vector v1 = new Vector(100);

try{

while(true){

for(int i = 0;i<10000;i++){

Vector v = new Vector();

v.add("asaaghvghgsahjshaghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hghsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaddddddddddddddddddddddddddddddddddddddddddddddddddd");

v1.add(v);

}}

}catch(Throwable t){t.getMessage();}

//String l = null;

//try{

//System.out.println(l.length());

//}catch(Exception e){}

System.out.println("AFTER CATCH");

}

}

[882 byte] By [mahiva] at [2007-11-27 9:22:49]
# 1
t.getMessage() does nothing.You may print the return of getMessage or execute printStackTrace() .catch Throwable orError works.
pbulgarellia at 2007-7-12 22:17:31 > top of Java-index,Core,Core APIs...
# 2

Yes, that is fine , but my query is that

1. if we write catch(Throwable e), then the statement after catch blockis not executed,

but if I write catch(Error e ) then it gets executed, why so?

2. If I remove the code to display free memory then the statement after catch block isn't executed in any case. why so?

import java.util.*;

class TestExcep

{

public static void main(String[] args)

{Vector v1 = new Vector(100);

try{

Runtime r =Runtime.getRuntime();

while(true){

System.out.println(r.freeMemory());

for(int i = 0;i<10000;i++){

Vector v = new Vector();

v.add("asaaghvghgsahjshaghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh hghsaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaddddddddddddddddddddddddddddddddddddddddddddddddddd");

v1.add(v);

}}

}catch(Throwable t){t.getMessage();}

System.out.println("AFTER CATCH");

}

}

mahiva at 2007-7-12 22:17:31 > top of Java-index,Core,Core APIs...
# 3
Your code works as expected for me. I suspect that in your case the pointless t.getMessage(); may throw OutOfMemoryError again itself. Try without it.
ejpa at 2007-7-12 22:17:31 > top of Java-index,Core,Core APIs...
# 4
i tried but this is not working, moreover if I comment System.out.println(r.freeMemory()); then the stament after catch block is not printed in case of Error also ?
mahiva at 2007-7-12 22:17:31 > top of Java-index,Core,Core APIs...
# 5
You got a OutOfMemory, what you expect?Your app is over, it can't do nothing, because it have no more memory.So...Why show free memory ?Why try to do something when you get a OutOfMemory ?Maybe its better search your memory leak....
pbulgarellia at 2007-7-12 22:17:31 > top of Java-index,Core,Core APIs...