This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra). *** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***

Tuesday, January 5, 2010

3 - Forcing to catch Error

  • We would like to simulate the well know "out of memory" error !
  • Is it an error we can catch ?
  • Is it an Exception ?
  • Is it a RuntimeException ? RuntimeException is a subclass of Exception...
class Exception1 {

    public static void main(String[] args) {
   
        try {
            long[][] arrayLong = new long[100000] [];
            for(int i=0;i < arrayLong.length; i++) {
                arrayLong[i] = new long[100000];
            }
        } catch(Error e) {
            System.out.println("type Error was caught !!!");
            System.out.println(e.getMessage());
        } finally {
            System.out.println("in finally...");
        }
    }
}

/*
rudy@vsolutions:~/Documents/SCP6/CH5$ java Exception1
type Error was caught !!!
Java heap space
in finally...
*/
  • We can catch Error !
  • It is NOT an Exception.
  • It is not a Runtime Exception !

No comments:

Post a Comment

Followers