Exception Handling in Java Part-4

Final, Finally and Finalize in Java

Final: Final is a modifier applicable for classes, methods and variables. If we declare any class as final then its child class creation is not possible. If we declare any method as final then it can not be overrided. Also if we declare any variable as final then it is treated as constant and hence we cannot change its value.

Finally: It is block always associated with try and catch blocks. It is basically used to keep the clean up code of the program. It is always executed whether any exception is thrown or not.

Finalize: It is a method which should execute by garbage collector before destroying any object to perform clean up activities.


Possible Combinations of try catch finally

case-1: Valid

try
{
}
catch(xx e)
{
}

case-2: Valid

try
{
}
catch(xx e) //child class
{
}
catch(xx e) //parent class
{
}

case-3: Valid

try
{
}
finally
{
}

case-4: Invalid

try
{
}

case-5: Invalid

catch(xx e)
{
}

case-6: Invalid

finally
{
}

case-7: Invalid

try
{
}
//code
catch(xx e)
{
}

case-8: Valid

try
{
}
catch(xx e)
{
}
//code

case 9: Invalid

try
{
}
catch(xx e)
{
}
//code
finally
{
}

case-10: Invalid

try
{
}
catch(xx e)
{
}
finally
{
}
finally
{
}

case-11: Invalid

try
{
}
catch(Exception e)
{
}
catch(Exception e)
{
}

case-12: Valid

try
{
    try 
    {
    }
    catch(xx e)
    {
    }
}
catch(xx e)
{
}

case-13: Valid

try
{
}
catch(xx e)
{
    try
    {
    }
    catch(xx e)
    {
    }
}
finally
{
}

case-14: Valid

try
{
}
catch(xx e)
{
}
finally
{
    try
    {
    }
    catch(xx e)
    {
    }
}

Case-15: Invalid

try
{
}
finally
{
}
catch(xx e)
{
}

Control Flow in try catch finally

try
{
    Statement1;
    Statement2;
    Statement3;
}
catch(Exception ex)
{
    Statement4;
}
finally
{
    Statement5;
}
Statement6;

Now in the above code there can be certain cases as below in which it get execute successfully.

Case-1: If the program do not throws exception in try block so statement 1, 2, 3, 5, 6 will execute and the program will terminate normally.

Case-2: If the program throws exception at statement 2 and the corresponding catch block matches then rest code successfully execute and then statement 1, 4,5 and 6 will execute and then program will terminate normally.

Case-3: If the program throws exception at statement 2 and the corresponding catch block does not match then statement 1 and 5 will execute and the program will abnormally terminate.

Case-4: If the program throws exception at statement 4 then statement 1, 2, 3, 4 and 5 will execute and the program will terminate normally.

Case-5: If the program throws exception at statement 5 then statement 1, 2 and 3 will execute and the program will terminate abnormally.

Case-6: If the program throws exception at statement 6 then statement 1, 2, 3 and 5 will execute and the program will terminate abnormally.


Keyword throw

In some situations a programmer may want to create an exception object manually and then handover the object to JVM manually. To perform such kind of operation we use throw keyword. Syntax:

throw new exception_class(argument);

For example,

throw new ArithmeticException("/ by zero");

Primary purpose of throw keyword is to handover created exception object manually to JVM. Following 2 program have exactly same output,

public class ExampleOne
{
    public static void main(String []args)
    {
        System.out.println(10/0);
    }
}

Note- In the above example, we create object of the ArithmeticException internally and hand it over to the JVM by the main.

public class ExampleTwo
{
    public static void main(String []args)
    {
        throw new ArithmeticException("/ by zero");
    }
}

Note- In the above example, we created object of the ArithmeticException and handed over to the JVM by the throw keyword.

Leave a Comment

Your email address will not be published. Required fields are marked *