错误与Java中的异常处理有关

huangapple go评论108阅读模式
英文:

error related to exception handling in java

问题

  1. 如何解决这个错误我尝试使用`throws`来抛出`FileNotFoundException`但错误仍然相同
  2. 编译时错误"默认构造函数无法处理隐式超级构造函数抛出的异常类型。必须定义显式构造函数。"
  3. 代码
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8. public class FileOne {
  9. Scanner sc = new Scanner(System.in);
  10. String file_name = sc.nextLine();
  11. File obj = new File(file_name);
  12. Scanner reader_obj = new Scanner(obj); // <--- 此行出错
  13. public static void main(String args[]) {
  14. FileOne f = new FileOne();
  15. f.create();
  16. f.writeFile();
  17. f.readFile();
  18. }
  19. void create() {
  20. try {
  21. System.out.println("输入文件名");
  22. if (obj.createNewFile()) {
  23. System.out.println("文件名为" + obj.getName());
  24. } else {
  25. System.out.println("已存在");
  26. }
  27. } catch (IOException e) {
  28. System.out.println("创建时发生错误");
  29. }
  30. }
  31. // 写入文件的方法
  32. void writeFile() {
  33. try {
  34. FileWriter w = new FileWriter(obj);
  35. w.write("现在正在学习文件");
  36. w.close();
  37. } catch (IOException e) {
  38. System.out.println("写入文件时发生异常");
  39. }
  40. }
  41. // 读取文件的方法
  42. /* 使用Scanner类读取创建的文本文件的内容 */
  43. void readFile() {
  44. while (reader_obj.hasNextLine()) {
  45. String data = reader_obj.nextLine();
  46. System.out.println(data);
  47. }
  48. reader_obj.close();
  49. }
  50. }
英文:

How to resolve this error? I have tried using throws to throw FileNotFoundException but still same error.

Compile-Time Error : "Default constructor cannot handle exception type Exception thrown by the implicit super constructor. Must define an explicit constructor "

CODE :

  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. public class FileOne {
  6. Scanner sc = new Scanner(System.in);
  7. String file_name = sc.nextLine();
  8. File obj = new File(file_name);
  9. Scanner reader_obj = new Scanner(obj); // &lt;--- error in this line
  10. public static void main(String args[]) {
  11. FileOne f = new FileOne();
  12. f.create();
  13. f.writeFile();
  14. f.readFile();
  15. }
  16. void create() {
  17. try {
  18. System.out.println(&quot;Enter a file name&quot;);
  19. if (obj.createNewFile()) {
  20. System.out.println(&quot;file name is&quot; + obj.getName());
  21. } else {
  22. System.out.println(&quot;Already exists&quot;);
  23. }
  24. } catch (IOException e) {
  25. System.out.println(&quot;error occured while creating&quot;);
  26. }
  27. }
  28. //method to write in file
  29. void writeFile() {
  30. try {
  31. FileWriter w = new FileWriter(obj);
  32. w.write(&quot;Learning files now&quot;);
  33. w.close();
  34. } catch (IOException e) {
  35. System.out.println(&quot;Exception occured while writing a file&quot;);
  36. }
  37. }
  38. //method to read
  39. /* use the Scanner class to read the contents of the text file created */
  40. void readFile() {
  41. while (reader_obj.hasNextLine()) {
  42. String data = reader_obj.nextLine();
  43. System.out.println(data);
  44. }
  45. reader_obj.close();
  46. }
  47. }

答案1

得分: 1

Scanner reader_obj = new Scanner(obj); 这一行代码在默认构造函数中隐式调用,可能会抛出一个受检异常 FileNotFoundException,必须进行处理。

一种处理的方式是显式定义一个无参构造函数:

  1. public FileOne() throws FileNotFoundException {
  2. }

不过,如果你这样做的话,出于清晰起见,你应该考虑将成员变量的初始化也移到这个构造函数中。

英文:

The line Scanner reader_obj=new Scanner(obj);, which is implicitly called by the default constructor, may throw a FileNotFoundException, which is a checked exception and must be handled.

One way of doing so is explicitly defining a no-arg constructor:

  1. public FileOne() throws FileNotFoundException {
  2. }

Although, if you're going to do that, you should consider moving the members' initialization in to it for clarity's sake.

答案2

得分: 1

错误已解决:

  1. 我在 main()reading() 方法中使用了 throws 来抛出异常。
  2. 使用了 FileReader 类来从给定的输入文件中读取数据

最终代码:

  1. public class FileOne {
  2. Scanner sc = new Scanner(System.in);
  3. String file_name = sc.nextLine();
  4. File obj = new File(file_name);
  5. // 用于创建文件的方法
  6. void create() {
  7. try {
  8. if (obj.createNewFile()) {
  9. System.out.println("文件名为:" + obj.getName());
  10. } else {
  11. System.out.println("已存在");
  12. }
  13. } catch (IOException e) {
  14. System.out.println("创建文件时出错");
  15. }
  16. }
  17. // 用于写入文件的方法
  18. void writeFile() {
  19. try {
  20. FileWriter w = new FileWriter(obj);
  21. w.write("现在学习文件");
  22. w.close();
  23. } catch (IOException e) {
  24. System.out.println("写入文件时出现异常");
  25. }
  26. }
  27. void reading() throws FileNotFoundException, IOException {
  28. FileReader reader = new FileReader(file_name);
  29. int i;
  30. while ((i = reader.read()) != -1) {
  31. System.out.print((char) i);
  32. }
  33. reader.close();
  34. }
  35. public static void main(String args[]) throws FileNotFoundException, IOException {
  36. FileOne f = new FileOne();
  37. f.create();
  38. f.writeFile();
  39. f.reading();
  40. }
  41. }

请注意,翻译只针对代码部分,不包括您提供的说明性文本。如果您需要进一步的帮助,请随时提问。

英文:

Errors Resolved:

  1. I used throws to throw exceptions in main() and reading() method.
  2. Used FileReader class to read the data from the given input file

Final Code:

  1. public class FileOne {
  2. Scanner sc=new Scanner(System.in);
  3. String file_name=sc.nextLine();
  4. File obj=new File(file_name);
  5. //method for creating a file
  6. void create(){
  7. try{
  8. if(obj.createNewFile()){
  9. System.out.println(&quot;file name is&quot;+obj.getName());
  10. }
  11. else{
  12. System.out.println(&quot;Already exists&quot;);
  13. }
  14. }
  15. catch(IOException e){
  16. System.out.println(&quot;error occured while creating&quot;);
  17. }
  18. }
  19. //method to write in file
  20. void writeFile(){
  21. try{
  22. FileWriter w=new FileWriter(obj);
  23. w.write(&quot;Learning files now&quot;);
  24. w.close();
  25. }
  26. catch(IOException e){
  27. System.out.println(&quot;Exception occured while writing a file&quot;);
  28. }
  29. }
  30. void reading() throws FileNotFoundException,IOException{
  31. FileReader reader=new FileReader(file_name);
  32. int i;
  33. while((i=reader.read())!=-1){
  34. System.out.print((char)i);
  35. }
  36. reader.close();
  37. }
  38. public static void main(String args[])throws FileNotFoundException,IOException{
  39. FileOne f=new FileOne();
  40. f.create();
  41. f.writeFile();
  42. f.reading();
  43. }
  44. }

答案3

得分: 0

添加如下构造函数:

  1. public FileOne() throws FileNotFoundException {
  2. }

将您的 void main() 编辑如下(您还需要从 main 方法中抛出异常):

  1. public static void main(String args[]) throws FileNotFoundException {
  2. FileOne f = new FileOne();
  3. f.create();
  4. f.writeFile();
  5. f.readFile();
  6. }
英文:

Add the constructor as below :

  1. public FileOne () throws FileNotFoundException {
  2. }

Edit your void main () as below (You need to throw the exception from main as well) :

  1. public static void main(String args[]) throws FileNotFoundException {
  2. FileOne f = new FileOne();
  3. f.create();
  4. f.writeFile();
  5. f.readFile();
  6. }

huangapple
  • 本文由 发表于 2020年8月31日 13:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63665113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定