Recent

2.finally Block

finally Block – Exception handling


The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.
“finally” is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
The runtime system always executes the statements within the finally block regardless of what happens within the try block. So it’s the perfect place to perform cleanup.
The finally block is a key tool for preventing resource leaks. When closing a file or otherwise recovering resources, place the code in a finally block to ensure that resource is always recovered.

Some Rules associated with finally block:
·         Finally block is always executed. If it is with try exception does not matter here.
·         Finally block is never used to catch any exception.
·         We can do 3 types of pairing try-catch, try-finally, try-catch-finally(most used).
·         Now we can have a try block with only finally block without a catch block.
·         We can have only one finally block with a single try block. This cannot be used as catch (multiple).

Programming Example
Example: Simple try-catch-finally example


class Temp
{

public static void main(String[] s)
{
try
{
int x = 10/s.length;
System.out.println(x);

int z[] = new int[s.length];
z[10] = 100;
}

catch(ArithmeticException e)
{
System.out.println(e+ " Catch block");
}

//this block will get execute at any cost
finally
{
System.out.println("Finally Block");
}
}}

/* Ouptut
java.lang.ArithmeticException: / by zero Catch block
Finally Block
*/

Example 2: Proof – finally method executes at any cost even if the program terminates with exception


class Temp
{

public static void main(String[] s)
{
try
{
int x = 10/s.length;
System.out.println(x);

int z[] = new int[s.length];
z[10] = 100;
}

//this block is not able to catch arithmetic exceptions
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e+ " Catch block");
}

//this will be executed even after the program
//terminates from try block due to exception
finally
{
System.out.println("Finally Block");
}
}}
Output:
Example 3: try-finally block (No catch block)


class Temp
{

public static void main(String[] s)
{
try
{
System.out.println("Try Block");
}

//this will be executed even after the program
//terminates from try block due to exception
finally
{
System.out.println("Finally Block");
}
}}

/*Output
Try Block
Finally Block
*/

Question 4: What will happen if both try and finally block returns a value?
>the statement in finally block will get executed at any cost.


class Temp
{
int show()
{

try
{
return 10;
}

catch(ArithmeticException e)
{
System.out.println(e);
}

finally
{
return 20;
}
}

public static void main(String s[])
{
Temp t = new Temp();
int x = t.show();
System.out.println(x);
}}

/* Output
20
*/

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India