gpt4 book ai didi

JAVA。我收到 'unreported exception' 编译器错误

转载 作者:行者123 更新时间:2023-12-01 16:48:58 24 4
gpt4 key购买 nike

我正在尝试编译此代码,但它始终出现错误,

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com