class Allocate
{
public static void main(String[] args)
{
try
{
long data[] = new long[1000000000];
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block will execute always.");
}
}
}
Program Explanation Step-by-Step:
This Java program illustrates the use of try, catch and finally blocks. It tries to get a large array and handle exceptions if encountered during allocation. The final block ensures that some code will always be executed, even if an exception was thrown.
1. The try Block
try {
long data[] = new long[1000000000];
}
- Array Allocation: Inside the try block, an attempt is made to allocate an array of type long with 1 billion elements: long data[] = new long[1000000000];.
- Memory Allocation: Creating this big array consumes a huge amount of memory. For most systems, this will be bigger than the heap, so an OutOfMemoryError or some other memory-related exception will occur.
- Possible Error: The amount of memory necessary to create this array is too large to fit in memory, so an exception should be thrown at runtime.
2. The catch Block
catch (Exception e) {
System.out.println(e);
}
- Exception Handling: If an exception occurs in the try block (for example, an OutOfMemoryError or other exceptions), it is caught by the catch block.
- Exception Output: The printed exception object e will print the details of its type.
- In this case, the output of the above statement must be error messages due to the allocation memory error, or in other words, something about java.lang.OutOfMemoryError.
3. The finally Block
finally {
System.out.println(\”finally block will execute always.”);
}
- A Always Executes: The finally block will always be executed irrespective of whether an exception occurs or not.
- Purpose: Most commonly utilized for cleanup purposes, such as closing resources like files, database connections, etc. It just prints a message here: \”finally block will execute always.\”
- Execution: The exception has already been handled in the catch block; however, the finally block is still executed after the exception is handled.
Key Points:
- try Block: Used to enclose code that may have the potential to throw an exception.
- catch Block: To catch and handle exceptions thrown by the try block.
- finally Block: It is guaranteed to run regardless of whether an exception was thrown or not.
Output:
For such a huge size of the array and assuming that the system might not have enough heap space to allocate this array, there’s a high chance of getting an OutOfMemoryError. This exception will be caught in the catch block, and the message will be printed. Now, the code in the finally block will be executed in any case. It will print a message.
java.lang.OutOfMemoryError: Java heap space
finally block will execute always.
Output Explanation:
- java.lang.OutOfMemoryError: Java heap space: It’s the exception message. So the program attempted to allocate memory beyond the amount that is present in the heap.
- finally block will execute always.: The reason why it’s being printed is that, by the definition of Java, finally blocks always execute after the try and catch blocks no matter what is the exception thrown.
- This program demonstrates a memory issue for trying to allocate an array which is too big, and hence it throws a Java exception. It also discusses the use of try, catch, and finally to handle this exception and run certain code anyway, even after an error happens.