当超类方法在覆盖它时引发异常时,您需要遵循某些规则。
如果超类方法引发某些异常,则子类中的方法应引发相同的异常或其子类型。
在下面的示例中,readFile()
超类的方法将引发IOEXception,readFile()
而子类的方法将引发FileNotFoundException异常。
由于FileNotFoundException异常是IOException的子类型,因此该程序得以编译和执行,没有任何错误。
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super{ public String readFile(String path) throws IOException{ throw new IOException(); } } public class ExceptionsExample extends Super { @Override public String readFile(String path) throws FileNotFoundException { Scanner sc = new Scanner(new File("E://test//sample.txt")); String input; StringBuffer sb = new StringBuffer(); while (sc.hasNextLine()) { input = sc.nextLine(); sb.append(" "+input); } return sb.toString(); } public static void main(String args[]) { String path = "E://test//sample.txt"; ExceptionsExample obj = new ExceptionsExample(); try{ System.out.println(obj.readFile(path)); } catch(FileNotFoundException e){ System.out.println("Make sure the specified file exists"); } } }
输出结果
nhooo.com is an E-learning company that set out on its journey to provide knowledge to that class of readers that responds better to online content. With nhooo.com, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at nhooo.com, we created our subscription based premium product called Tutorix to provide Simply Easy Learning in the best personalized way for K-12 students, and aspirants of competitive exams like IIT/JEE and NEET.
以相同的方式,如果子类抛出与超类相同的异常,则程序将成功编译并执行。
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super{ public void sampleMethod()throws FileNotFoundException{ System.out.println("Method of superclass"); } } public class ExceptionsExample extends Super{ public void sampleMethod()throws FileNotFoundException { System.out.println("Method of Subclass"); } public static void main(String args[]) { ExceptionsExample obj = new ExceptionsExample(); obj.sampleMethod(); } }
输出结果
Method of Subclass
如果超类方法抛出某些异常,则子类中的方法不应抛出其超类型。
在下面的示例readFile()
中,超类的方法将引发FileNotFoundException异常,readFile()
而子类的方法将引发IOException,这是FileNotFoundException的超类型。
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super{ public String readFile(String path)throws FileNotFoundException{ throw new FileNotFoundException(); } } public class ExceptionsExample extends Super{ @Override public String readFile(String path)throws IOException { //方法主体......- } }
在编译时,上述程序为您提供以下输出-
输出结果
ExceptionsExample.java:13: error: readFile(String) in ExceptionsExample cannot override readFile(String) in Sup public String readFile(String path)throws IOException { ^ overridden method does not throw IOException 1 error
如果超类方法引发某些异常,则可以覆盖它而不用划行任何异常。
在下面的示例sampleMethod()
中,超类的方法将引发FileNotFoundException异常,并且该sampleMethod()
方法完全不会引发任何异常。该程序仍然可以编译并执行,没有任何错误。
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super{ public void sampleMethod()throws FileNotFoundException{ System.out.println("Method of superclass"); } } public class ExceptionsExample extends Super{ public void sampleMethod() { System.out.println("Method of Subclass"); } public static void main(String args[]) { ExceptionsExample obj = new ExceptionsExample(); obj.sampleMethod(); } }
输出结果
Method of Subclass