各位大虾:
我的A.java代码如下:
import com.abc.*;
...others
try
{
...
myfunc(it);
}catch(Exception e)
{
do something here
}
其中myfunc()方法是com.abc.Test类的一个方法:
myfunc(int i)
{
try
{
}catch(IOException ex)
{
System.out.println( "myfunc is wrong ");
}
}
请问:如果在A.java里执行myfunc()方法时出现异常,A.java能捕获吗?如何做?
谢谢
------解决方案--------------------
不能catch要想catch住必须在 myfunc(int i)
中重新throw
------解决方案--------------------
这样做:
myfunc(int i) throws IOException
{
}
或:
myfunc(int i) throws IOException
{
try
{
}catch(IOException ex)
{
System.out.println( "myfunc is wrong ");
throw ex;
}
}
------解决方案--------------------
楼上正解
------解决方案--------------------
myfunc 中throws出去A.java就能捕获
------解决方案--------------------
查看下是否真的抛出了这个异常
------解决方案--------------------
在myfunc中已经捕获了
修改:
myfunc(int i) throws Exception
{
try
{
}catch(IOException ex)
{
System.out.println( "myfunc is wrong ");
throw new Exception();
}
}
------解决方案--------------------
只是需要将 Exception 重新抛出而已.
------解决方案--------------------
如果异常需要在catch后重新被throw出去,那你又何必catch呢?