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-15]

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");
}
}
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....