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

标签 java exception checked-exceptions catch-exception

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

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/

相关文章:

java - 如何编写链式异常 Java 类

java - 在原型(prototype)制作时自动添加/删除已检查的异常

java - oncreate 中的 Textwatcher android

java - "java.lang.NoSuchFieldError: NONE"与 Spring 3、maven、JPA、c3p0 hibernate

asp.net - Application_Error 在不同线程上触发

java - 当 Arraylist 大小为 139 时,索引 6 上出现 IndexOutOfBoundsException

java - JLS 的哪些部分证明能够像未经检查一样抛出已检查异常?

java - react 获取错误/响应捕获不起作用

java - 对集合使用 hashCode() 和 equals() 的多个替代方案

java - 如何配置项目(取决于 maven/web.xml/servlet API)AJAX 调用不包含项目名称?