Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 异常处理_Java_Exception_Exception Handling_Io - Fatal编程技术网

Java 异常处理

Java 异常处理,java,exception,exception-handling,io,Java,Exception,Exception Handling,Io,是否有可能在抛出异常的方法之外捕获异常 例如: public double[] readFile(String filename) throws IOException { File inFile = new File(filename); Scanner in = new Scanner(inFile); try { readData(in); return data; } finally {

是否有可能在抛出异常的方法之外捕获异常

例如:

public double[] readFile(String filename) throws IOException
    {
    File inFile = new File(filename);
    Scanner in = new Scanner(inFile);
    try
    {
        readData(in);
        return data;
    }
    finally
    {
        in.close();
    }
}
如何在
main
方法中捕获IOException
我可以只做
catch(IOException){}

是的,我可以这样做,捕捉
someMethod()
方法中抛出的异常:

   public double[] readFile(String filename) throws IOException
    {
    ...
    }
在另一种方法中,例如:

  public void someMethod(){
   try
    {
   readFile(in);
   return data;
    }catch(IOException io){
   }
    ...
  }

是的,您可以捕获
someMethod()
方法中抛出的异常,如下所示:

   public double[] readFile(String filename) throws IOException
    {
    ...
    }
在另一种方法中,例如:

  public void someMethod(){
   try
    {
   readFile(in);
   return data;
    }catch(IOException io){
   }
    ...
  }

在这个方法中,您不需要使用
try/catch
语句,因为您不希望在内部处理异常,而是希望抛出异常。(这就是
抛出的
关键字的作用)

所以你可以这样做:

public double[] readFile(String filename) throws IOException
{
    File inFile = new File(filename);
    Scanner in = new Scanner(inFile);

    readData(in);
    // If everything goes normally, the execution flow shall pass on to
    // the next statements, otherwise if an IOException is thrown, it shall
    // be handled by the caller method (main)

    in.close();
    return data;
}
&在
main
方法中,处理潜在的异常:

try {
    double[] result = readFile("filename.ext");
    // ...
}
catch(IOException e) {
    // Handle the exception
}

在这个方法中,您不需要使用
try/catch
语句,因为您不希望在内部处理异常,而是希望抛出异常。(这就是
抛出的
关键字的作用)

所以你可以这样做:

public double[] readFile(String filename) throws IOException
{
    File inFile = new File(filename);
    Scanner in = new Scanner(inFile);

    readData(in);
    // If everything goes normally, the execution flow shall pass on to
    // the next statements, otherwise if an IOException is thrown, it shall
    // be handled by the caller method (main)

    in.close();
    return data;
}
&在
main
方法中,处理潜在的异常:

try {
    double[] result = readFile("filename.ext");
    // ...
}
catch(IOException e) {
    // Handle the exception
}

当然,如果方法本身没有捕获异常,则会将异常传播到调用方。或者您可以
catch
it,然后(在catch块中)重新播放它,以便调用方也可以获取异常。但不要让catch块为空:)如何重新引发异常?它是否与仅仅抛出它相同,但在catch中包含?当然,如果方法本身没有捕获异常,它将被传播到调用方。或者您可以
catch
it,然后重新抛出它(在catch块中),以便调用方也获得它。但不要让catch块为空:)如何重新引发异常?这和扔球一样,但却被抓着了吗?好得多,这就是我想要的!好多了,这就是我要找的!