Java中使用方法重写进行异常处理
在子类中,我们可以覆盖超类的方法并提供不同的实现。但是,如果超类中的该方法使用throws子句声明异常,该怎么办。子类中的重写方法是否也继承那些异常声明,可以在重写方法中重写那些异常。让我们尝试通过Java中的方法重写在此异常处理后找到这些问题的答案。
在通过方法重写进行异常处理的情况下遵循的规则
在覆盖一种方法时,有一些Java异常处理规则,具体如下:
- 如果超类中的方法未声明任何异常,则子类中被重写的方法尽管可以声明未检查的异常,但无法声明任何检查的异常。
- 如果超类中的方法有一些使用throws子句声明的异常,则子类重写方法中有三个选项。子类方法可以声明与父类方法相同的异常。
- 子类方法可以声明父类方法中声明的异常的任何子异常。但是它不能在层次结构中声明更高的任何异常。例如,如果父类方法声明IOException,则子类方法可以声明FileNotFoundException,因为FileNotFoundException是IOException的子类。但是尝试使用子类方法声明Exception会导致错误,因为Exception类是IOException的父类。
- 覆盖的子类方法根本不会声明任何异常。
异常处理方法重写示例
让我们看一下上面概述的场景的一些例子,以使其更清楚。
如果超类中的方法未声明任何异常
在这里,我们有一个Shape类,它是一个父类,它具有一个area()方法。有一个子类Circle,其中area()方法被覆盖。如果子类Circle中的area()方法使用throws子句声明了IllegalArgumentException,则不会出错,因为IllegalArgumentException是未经检查的异常。
class Shape {
public void area(int side){
System.out.println("Calculate area");
}
}
public class Circle extends Shape {
// Overridden method
public void area(int radius) throws IllegalArgumentException{
System.out.println("Calculate area");
double area = Math.PI * Math.pow(radius, 2);
System.out.println("Circle area " + area);
}
public static void main(String[] args) {
Shape shape;
shape = new Circle();
shape.area(5);
}
}
如果将声明的异常更改为ParseException,则将收到编译器错误,因为ParseException是已检查的异常。
如果超类中的方法声明了异常
现在,让我们看看超类方法声明异常时的可能性。
1子类重写方法可以声明一个异常,它是超类方法的子类型。
class Read {
public void readFile() throws IOException{
System.out.println("read file");
}
}
public class FileRead extends Read {
// Overridden method
public void readFile() throws FileNotFoundException{
File file = new File("D://test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
}
public static void main(String[] args) {
FileRead fr = new FileRead();
try {
fr.readFile();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在上面的父类Read中的代码中,有一个方法readFile()声明了IOException。我们将在子类FileRead中重写此方法,并在其中声明FileNotFoundException。可以,因为FileNotFoundException是IOException的子类。
在子类方法中,如果将抛出的异常更改为Exception,则由于Exception是IOException的超级类型,因此将收到错误消息。
public void readFile() throws Exception{
File file = new File("D://test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
}
输出:
Exception Exception is not compatible with throws clause in Read.readFile() FileRead.java
2子类重写方法完全不引发任何异常,这也可以。
class Read {
public void readFile() throws IOException{
System.out.println("read file");
}
}
public class FileRead extends Read {
// Overridden method
public void readFile(){
File file = new File("D://test.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
FileRead fr = new FileRead();
fr.readFile();
}
}
子类中的readFile()方法并未声明任何异常,尽管父类方法却声明了任何异常。
3Sub类方法抛出的异常与父类方法相同。
class Read {
public void readFile() throws IOException{
System.out.println("read file");
}
}
public class FileRead extends Read {
// Overridden method
public void readFile() throws IOException{
File file = new File("D://test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
}
public static void main(String[] args) {
FileRead fr = new FileRead();
try {
fr.readFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
主题为Java中的方法异常处理。如果缺少某些内容,或者我们需要分享一些有关此主题的信息,请发表评论。

