0% found this document useful (0 votes)
37 views

Object-Oriented Programming (CS F213) : BITS Pilani

The document discusses the use of the finally clause in exception handling in Java. It explains that a finally block is used to perform cleanup operations and is guaranteed to execute whether an exception is thrown or not. It can be placed immediately after the try block or after all catch blocks. An example demonstrates finally blocks executing after both inner and outer try/catch blocks. Important facts are that subclasses must be caught before parent classes, and overridden methods cannot throw broader exceptions than the parent method.

Uploaded by

SAURABH MITTAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Object-Oriented Programming (CS F213) : BITS Pilani

The document discusses the use of the finally clause in exception handling in Java. It explains that a finally block is used to perform cleanup operations and is guaranteed to execute whether an exception is thrown or not. It can be placed immediately after the try block or after all catch blocks. An example demonstrates finally blocks executing after both inner and outer try/catch blocks. Important facts are that subclasses must be caught before parent classes, and overridden methods cannot throw broader exceptions than the parent method.

Uploaded by

SAURABH MITTAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Object-Oriented Programming (CS F213)

Module V: Exceptions in Java


CS F213 RL 13.4: finally clause in Java

BITS Pilani Dr. Pankaj Vyas


Department of Computer Science, BITS-Pilani, Pilani Campus
CS F213 RL 13.4 : Topics

Use of finally clause in Exception Handling


Some Important Facts About Exceptions

2 Object-Oriented Programming (CS F213)


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.

3 Object-Oriented Programming (CS F213)


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

4 Object-Oriented Programming (CS F213)


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
5 Object-Oriented Programming (CS F213)
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]

6 Object-Oriented Programming (CS F213)


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
7 Object-Oriented Programming (CS F213)
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
8 Object-Oriented Programming (CS F213)
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

9 Object-Oriented Programming (CS F213)


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
{
}
}

10 Object-Oriented Programming (CS F213)


Thank You

11 Object-Oriented Programming (CS F213)

You might also like