首页 > Java > java教程 > 正文

在Java中,关于方法覆盖,异常处理的规则是什么?

WBOY
发布: 2023-09-06 18:29:13
转载
846人浏览过

在java中,关于方法覆盖,异常处理的规则是什么?

当覆盖超类方法时,如果该方法抛出异常,您需要遵循一定的规则。

应该抛出相同的异常或者子类型

如果超类方法抛出某个异常,子类中的方法应该抛出相同的异常或者它的子类型。

示例

在下面的示例中,超类的readFile()方法抛出了IOException异常,而子类的readFile()方法抛出了FileNotFoundException异常。

由于FileNotFoundException异常是IOException的子类型,所以该程序可以在没有任何错误的情况下编译和执行。

立即学习Java免费学习笔记(深入)”;

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");
      }
   }
}
登录后复制

输出

Tutorials Point 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 Tutorials Point, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at tutorialspoint.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.
登录后复制

示例

同样,如果子类抛出与超类相同的异常,则程序将成功编译并执行。

法语写作助手
法语写作助手

法语助手旗下的AI智能写作平台,支持语法、拼写自动纠错,一键改写、润色你的法语作文。

法语写作助手 31
查看详情 法语写作助手
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 {
      //method body ......
   }
}
登录后复制

编译时错误

编译时,上面的程序给出以下输出 -

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
登录后复制

以上就是在Java中,关于方法覆盖,异常处理的规则是什么?的详细内容,更多请关注php中文网其它相关文章!

相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号