import java.io.*;
class BBB
{
public static void main(String args[])
{
try
{
throw new IOException();
                        throw new FileNotFoundException();
                }catch(IOException e)
                   {}
                 catch(FileNotFoundException p)
                   {}
         }
}这样好象出错。。

解决方案 »

  1.   

    是阿。你这里throw new IOException();抛出了一个异常,而且是必须抛出的,下边那句就不会被执行了。java就会认为你写的语句有问题,你可以这样改下:
    if(true)
    throw new IOException();
    else
     throw new FileNotFoundException();
    放在不同的分支中,只让他执行一个就行了
      

  2.   


    当然,你捕捉异常的顺序得颠倒一下,不然后边的始终没事干。这样 catch(FileNotFoundException p)
                       {}catch(IOException e)
                       {}
      

  3.   

    哈 楼主的两个异常换下顺序就可以了import java.io.*;
    class BBB
    {
        public static void main(String args[])
        {
            try
            {
                throw new IOException();
                            throw new FileNotFoundException();
                    }catch(FileNotFoundException e)
                       {}
                     catch(IOException  p)
                       {}
             }
    }
    因为FileNotFoundException异常是IOException异常中的一种,你先捕捉了那个大范围的异常,下面小范围的当然就不能执行了,所以换一下位置就行了
      

  4.   

    import java.util.*;
    import java.io.*;
    public class UI
    {
    public static void f() throws IOException,FileNotFoundException
    {
        throw new IOException();
        throw new FileNotFoundException();
    }
    public static void main(String args[])
    {
    try
    {
    f();
    }
    catch(FileNotFoundException e)
    {
    System.out.println("sf");
    }
    catch(IOException e)
    {
    System.out.println("sdf");
    }

    }}
    这个程序可以捕获两个异常
      

  5.   

    throw new IOException();的异常范围大于 throw new FileNotFoundException();
    也就是说可能出现的FileNotFoundException();异常己被IOException()抛出,FileNotFoundException();存在没意义,就错了
      

  6.   

    可以的.或者直接在方法后面这样写:public static void main(String args[]) throws IOException,FileNotFoundException
    {}
      

  7.   

    答:当然出错。因为:第二条throw语句是死代码,永远不会得到执行。这个与IOException异常与FileNotFoundException异常谁大谁小无关。
      

  8.   

    说无关也不是很合适,在调试的时候你看不到FileNotFoundException出现是你预期看到的内容了,在这里不会对程序的主要路径产生影响,但是如果异常出现了,你可能就搞不清楚到底是什么异常了。