8 ThrowsClause
8 ThrowsClause
1 Throws Clause
Then, it must specify this behaviour so that callers of the method can
guard themselves against that exception
Compilation Error………why?
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
}