无法在java中的方法上存储消息,但有异常

无法在java中的方法上存储消息,但有异常,java,exception-handling,Java,Exception Handling,我的代码需要在getmessage上存储给MiExepcion的消息,但它不起作用。 代码是: package act5_3; public class act5_3 { public static void main(String[] args) { try { MiExcepcion e = new MiExcepcion("Este es mi propio error."); throw e; }

我的代码需要在getmessage上存储给MiExepcion的消息,但它不起作用。 代码是:

package act5_3;

public class act5_3 {
    public static void main(String[] args) {
        try {
            MiExcepcion e = new MiExcepcion("Este es mi propio error.");
            throw e;
        } catch (MiExcepcion e) {
            //Error in the next line
            //method in getmessage in class MiExcepcion cannot be applied to given types.
            System.out.println("Excepción: " + e.getmessage());
        }
    }
}

//Declarin Exception
class MiExcepcion extends Exception {
    public MiExcepcion(String message) {
        super(message);
    }

    //method to store given message
    public String getmessage(String message) throws MiExcepcion {
        return message;
    }
}

谢谢您的时间。

Exception类已经有了getMessage()方法。为什么不直接使用它呢

public class Foo {
   public static void main(String[] args) {
      try {
         MiExcepcion e = new MiExcepcion("Este es mi propio error.");
         throw e;
      } catch (MiExcepcion e) {
         System.out.println("Excepción: " + e.getMessage());
      }
   }
}

class MiExcepcion extends Exception {
   public MiExcepcion(String message) {
      super(message);
   }

   // Not needed
   // public String getmessage(String message)throws MiExcepcion
   // {
   // return message;
   // }
}

请注意,大小写很重要,
getmessage()
将不起作用,而
getmessage()
将起作用。

虽然您可以使用
Exception.getmessage
正如气垫船所说,您的代码问题对我来说似乎非常清楚

您声明了方法
getmessage(stringmessage)
,该方法接受字符串作为参数,但在主类中调用它时,您不会传递任何参数。令我惊讶的是,这段代码甚至正在编译。。。这可能就是抛出给您的异常的含义,它很可能是
NoSuchMethodException


下次请发布异常的堆栈跟踪。

您的代码没有任何缩进,这使得所有代码都左对齐,几乎不可能读取、理解和调试。请重新格式化您发布的代码,给它适当的缩进,通常每个块4个空格,并确保同一块上的所有代码都在相同的缩进级别。非常感谢您在这方面的合作,这可能会提高您获得适当和及时答案的机会。Exception类已经有了一个
getMessage()
方法。为什么不直接使用它呢?我知道,但我被要求为它创建一个方法。@JosuéZatarain:发布你作业的具体要求,因为我感觉你可能误解了你应该做的事情。同样,请更好地格式化您的代码。@JosuéZatarain:
公共字符串getMessage(){super.getMessage();}
如果您需要创建自己的方法,它可以正常工作。