Throws and Inheritance - Java Language Basics

Java examples for Language Basics:throw

Introduction

Any exception thrown by the subclass must be the same as the superclass or a subclass of that exception.

void readFields() throws IOException { 
} 

If this method is in a superclass and you override the method, this would not be allowed in the subclass:

void readFiles() throws SQLException { 
} 

SQLException is not a subclass of IOException, so this code will not compile.

But the method could throw FileNotFoundException, because that's a subclass of IOException.


Related Tutorials