Topics
• Use of finally clause in Exception Handling
• Some Important Facts About Exceptions
1 Object-Oriented Programming Using Java
Use of finally clause (statement)
• finally block in general used to perform certain house keeping
operations such as closing files or releasing system
resources.
• finally block may be added immediately after try block or after
the last catch block.
• finally block when present is guaranteed to execute
regardless of whether an exception is thrown or not.
• If required , finally block can be used to handle any exception
generated within a try block.
2 Object-Oriented Programming Using Java
finally clause Syntax
// Immediately After try() block // After the last catch() block
try try
{ {
………………….. …………………..
………………….. …………………..
} // End of try } // End of try
finally catch(Exception-Type-1 e) { … }
{ catch(Exception-Type-2 e) { … }
………………….. …
………………….. catch(Exception-Type-N e) { … }
} // End of finally finally
catch(Exception-Type-1 e) { … } {
catch(Exception-Type-2 e) { … } …………………..
… …………………..
catch(Exception-Type-N e) { … } } // End of finally
3 Object-Oriented Programming Using Java
finally clause Example
// File Name ExceptionDemo.java
class ExampleFinallyClause
{
public static void main(String args[])
{
int a=10;
int b = 20;
try // Outer Try
{
int b1=Integer.parseInt(args[0]);
int x = a/(a-b1);
try // Inner try
{
int y = b/(b-b1);
} // End of Inner try
finally
{
System.out.println("Inner Block executed");
} // End of finally clause
} // End of Outer try
finally
{
System.out.println("Outer Block executed");
} // End of finally clause
} // End of main() Method
} // End of class
4 Object-Oriented Programming Using Java
finally clause Example …
java ExampleFinallyClause
Outer Block executed
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 [Partial Output
Shown]
java ExampleFinallyClause 45
Inner Block executed
Outer Block executed
java ExampleFinallyClause 10
Outer Block executed
Exception in thread "main" java.lang.ArithmeticException: / by zero [Partial Output Shown]
java ExampleFinallyClause 20
Inner Block executed
Outer Block executed
Exception in thread "main" java.lang.ArithmeticException: / by zero [Partial Output Shown]
5 Object-Oriented Programming Using Java
Some Important Facts About
Exceptions
• Fact I : A super class exception type can catch all sub class
exceptions. So, while writing catch blocks , catch sub class
exceptions first and then super class exceptions
class AException extends RuntimeException { }
class BException extends AException { } RuntimeException
class CException extends AException { }
class Demo
{ AException
public static void main(String args[])
{
try BException CException
{
int a=10;
}
catch(AException e) { }
catch(BException e) { }
catch(CException e) { }
} // End of method
} // End of class
6 Object-Oriented Programming Using Java
Some Important Facts About
Exceptions
• Fact I : A super class exception type can catch all sub class
exceptions. So, while writing catch blocks , catch sub class
exceptions first and then super class exceptions
class AException extends RuntimeException { }
class BException extends AException { } RuntimeException
class CException extends AException { }
class Demo
{ AException
public static void main(String args[])
{
try BException CException
{
int a=10;
}
catch(BException e) { }
catch(CException e) {
catch(AException e) {
}
}
NO ERROR
} // End of method
} // End of class
7 Object-Oriented Programming Using Java
Some Important Facts About
Exceptions
• Fact II : An overridden method in sub-class can not throw an
exception broader and stronger than the method of the super class
// File Name : ExceptionDemo.java
import java.io.*;
class A
Compile-Time Error
{
public void display() throws IOException
ExceptionDemo.java:11:
{
display() in B cannot
}
override display() in A;
}
overridden method does
class B extends A
not throw
{
java.lang.Exception
public void display() throws Exception
public void display()
{
throws Exception
}
^
}
1 error
8 Object-Oriented Programming Using Java
Some Important Facts About
Exceptions
• Fact II : An overridden method in sub-class can not throw an
exception broader and stronger than the method of the super class
// File Name : ExceptionDemo.java
import java.io.*;
class A
{
public void display() throws RuntimeException
{
}
}
class B extends A
{
public void display() throws IOException
{
}
}
9 Object-Oriented Programming Using Java
Thank You
10 Object-Oriented Programming Using Java