为什么会抛出编译时错误?
Why it is throwing compile time error?
为什么这个程序抛出编译时错误,即使我已经声明了 Ari class 扩展异常 class.It 给我输出像 "unreported exception Ari; must be caught or declared to be thrown".
class Ari extends Exception{ }
public class Main
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C ");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Ari(); /* Line 22 */
}
}
这看起来像 Java,它使用 "checked exceptions"。由于您的方法可以抛出 Ari
异常(事实上,它 保证 ),方法签名必须声明:
public static void badMethod() throws Ari {
throw new Ari();
}
这建议使用此特定异常的可能性的代码,以便可以编写它来处理该异常。
为什么这个程序抛出编译时错误,即使我已经声明了 Ari class 扩展异常 class.It 给我输出像 "unreported exception Ari; must be caught or declared to be thrown".
class Ari extends Exception{ }
public class Main
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C ");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Ari(); /* Line 22 */
}
}
这看起来像 Java,它使用 "checked exceptions"。由于您的方法可以抛出 Ari
异常(事实上,它 保证 ),方法签名必须声明:
public static void badMethod() throws Ari {
throw new Ari();
}
这建议使用此特定异常的可能性的代码,以便可以编写它来处理该异常。