0% found this document useful (0 votes)
29 views14 pages

8 ThrowsClause

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)
29 views14 pages

8 ThrowsClause

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/ 14

Throws Clause

1 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Agenda

1 Throws Clause

2 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Throws Clause

3 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Using throws
 Sometimes, a method is capable of causing an exception that it does
not handle

 Then, it must specify this behaviour so that callers of the method can
guard themselves against that exception

 While declaring such methods, you have to specify what type of


exception it may throw by using the throws keyword

 A throws clause specifies a comma-separated list of exception types


that a method might throw:
 type method-name( parameter list) throws exception-list

4 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Using throws (Contd.).
class ThrowsDemo{
static void throwOne(){
System.out.println("Inside throwOne.");
throw new FileNotFoundException();
}
public static void main(String args[]){
throwOne();
}
}
What happens when this code is
compiled ?

Compilation Error………why?

5 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Implementing throws
import java.io.*;
class ThrowsDemo{
static void throwOne() throws
FileNotFoundException{
System.out.println("Inside throwOne.");
throw new FileNotFoundException();
}
public static void main(String args[]) {
try{
throwOne();
}
catch (FileNotFoundException e){
System.out.println("Caught " + e);
}
}
}

6 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Rule governing overriding method with throws

 The overriding method must NOT throw checked


exceptions that are new or broader than those declared by
the overridden method

For eg : A method that declares(throws) an SQLException


cannot be overriden by a method that declares an
IOException, Exception or any other exception unless it is a
subclass of SQLException

 In other words, if a method declares to throw a given


exception, the overriding method in a subclass can only
declare to throw the same exception or its subclass

 This rule does not apply for unchecked exceptions

7 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Quiz
 What will be the result, if we try to compile the following
code (FileNotFoundException is a subclass of IOException)
import java.io.*;
class Super {
void m1() throws FileNotFoundException {
FileInputStream fx = new
FileInputStream("Super.txt");
}
Yes, it will throw compilation Error
}
class Sub extends Super {
void m1() throws IOException {
FileInputStream fx = new
FileInputStream("Sub.txt");
}
}
8 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL
Quiz (Contd.).
 What will be the result, if we try to compile the following code
(FileNotFoundException is a subclass of IOException)
import java.io.*;
class Super {
void m1() throws IOException {
FileInputStream fx = new
FileInputStream("Super.txt");
}
No Error! Compilation successful
}
class Sub extends Super {
void m1() throws FileNotFoundException {
FileInputStream fx = new FileInputStream("Sub.txt");
}
}

9 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Quiz (Contd.).

• What will be the result, if we try to compile the following code

class Super {
void m1() throws ArithmeticException {
int x = 100, y=0;
int z=x/y;
System.out.println(z);
}
} No Error! Compilation successful
class Sub extends Super {
void m1() throws NumberFormatException {
System.out.println("Wipro");
}
}
10 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL
Quiz (Contd.).
 What will be the result, if we try to compile the following code
(FileNotFoundException & SQLException are not related
hierarchically)
import java.io.*;
import java.sql.*;
class Super {
void m1() throws FileNotFoundException {
FileInputStream fx = new FileInputStream("Super.txt");
}
It will throw compilation Error
}
class Sub extends Super {
void m1() throws SQLException {
FileInputStream fx = new FileInputStream("Sub.txt");
}
}
11 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL
Quiz (Contd.).
What will be the result, if we try to compile and execute the
following code
import java.io.*;
class Plane {
public Plane() throws IOException,
RuntimeException {
System.out.println(“Plane”);
}
}
class Jet extends Plane { }
public class Tester {
public static void main(String args[]) throws
IOException {
new Plane();
}
It will throw compilation Error
}

12 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Summary

In this session, you were able to :

 Learn about throws clause

13 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL


Thank You

14 © 2015 WIPRO LTD | WWW.WIPRO.COM | CONFIDENTIAL

You might also like