我正在尝试编译此代码,但它始终出现错误,
errThrower.java:37: error: unreported exception Exception; must be caught or declared to be thrown
throw new Exception();
这个异常是在callmethodErr()
中抛出的,我以为它已经在main中捕获了,但我不知道发生了什么。
谢谢大家。
import java.util.IllegalFormatConversionException;
public class errThrower
{
public static void main(String[] args)
{
try
{
callmethodErr();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void methodErr() throws Exception
{
System.out.println("error thrown from methodErr");
}
public static void callmethodErr()
{
try
{
methodErr();
}
catch (Exception e)
{
System.out.println("error thrown from callMethodErr");
throw new Exception();
}
}
}
最佳答案
这个方法:
public static void callmethodErr()
{
包含行:
throw new Exception();
但没有声明它抛出异常
,因此:
public static void callmethodErr() throws Exception
{
关于JAVA。我收到 'unreported exception' 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44498014/