有人可以指出这里有什么问题吗

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

Can someone please point out what's wrong here

问题

我确保包含了我认为所需的所有异常处理,但它仍然给了我这个错误:

异常 FileNotFoundException 已经被捕获
捕获(FileNotFoundException fnfe)

public class SNIDDb
{
  private char delimiter;
  private String name;
  private BufferedReader br;

  public SNIDDb(String name, char delimiter)
  {
    this.name=name;
    this.delimiter=delimiter;
    try
    {
      FileReader fr= new FileReader(name);
      br= new BufferedReader(fr);
    }
    catch(IOException i)
    {
      System.out.println(i.getMessage());
    }
    catch(FileNotFoundException fnfe)
    {
      System.out.print(fnfe.getMessage());
    }
  }
}
英文:

I made sure to have all the exceptions I think are needed but it keeps giving me this error:

exception FileNotFoundException has already been caught
catch(FileNotFoundException fnfe)

public class SNIDDb
{
  private char delimiter;
  private String name;
  private BufferedReader br;

  public SNIDDb(String name, char delimiter)
  {
    this.name=name;
    this.delimiter=delimiter;
    try
    {
      FileReader fr= new FileReader(name);
      br= new BufferedReader(fr);
    }
    catch(IOException i)
    {
      System.out.println(i.getMessage());
    }
    catch(FileNotFoundException fnfe)
    {
      System.out.print(fnfe.getMessage());
    }
  }

答案1

得分: 1

看起来FileNotFoundException扩展了IOException。尝试颠倒这两个catch语句,应该没问题。

类FileNotFoundException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.io.IOException
java.io.FileNotFoundException

请参阅https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html

英文:

It sounds like FileNotFoundException extends IOException. Try inverting the two catch statements and you should be good.

> Class FileNotFoundException
> java.lang.Object
> java.lang.Throwable
> java.lang.Exception
> java.io.IOException
> java.io.FileNotFoundException

See https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html

答案2

得分: 1

如果您查看FileNotFoundException的定义,您将看到它是

public class FileNotFoundException extends IOException {

因此,通过捕获基类IOException,实际上已经捕获了派生的异常,因此您看到的错误。

但是,如果您进一步查看您正在调用的FileReader构造函数,它只会抛出FileNotFoundException,所以实际上您不需要捕获IOException,可以删除该捕获子句并修复您的错误。

英文:

If you look at the definition of FileNotFoundException you will see it is

public class FileNotFoundException extends IOException {

So by catching the base class IOException you are already, in effect, catching the derived exception, hence the error you are seeing.

However, if you further look at the FileReader constructor you are calling, it only throws FileNotFoundException so you actually don't need to catch IOException in and can remove that catch clause and fix your error.

答案3

得分: 0

由于FileNotFoundException扩展了IOException,根据您的需求可以选择FileNotFoundException或IOException。

英文:

Since FileNotFoundException extends IOException, you can choose either FileNotFoundException or IOException based on your needs.

huangapple
  • 本文由 发表于 2020年5月30日 14:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/62098958.html
匿名

发表评论

匿名网友

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

确定