import java.io.*;
class BBB
{
public static void main(String args[])
{
try
{
throw new IOException();
throw new FileNotFoundException();
}catch(IOException e)
{}
catch(FileNotFoundException p)
{}
}
}这样好象出错。。
class BBB
{
public static void main(String args[])
{
try
{
throw new IOException();
throw new FileNotFoundException();
}catch(IOException e)
{}
catch(FileNotFoundException p)
{}
}
}这样好象出错。。
if(true)
throw new IOException();
else
throw new FileNotFoundException();
放在不同的分支中,只让他执行一个就行了
当然,你捕捉异常的顺序得颠倒一下,不然后边的始终没事干。这样 catch(FileNotFoundException p)
{}catch(IOException e)
{}
class BBB
{
public static void main(String args[])
{
try
{
throw new IOException();
throw new FileNotFoundException();
}catch(FileNotFoundException e)
{}
catch(IOException p)
{}
}
}
因为FileNotFoundException异常是IOException异常中的一种,你先捕捉了那个大范围的异常,下面小范围的当然就不能执行了,所以换一下位置就行了
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");
}
}}
这个程序可以捕获两个异常
也就是说可能出现的FileNotFoundException();异常己被IOException()抛出,FileNotFoundException();存在没意义,就错了
{}