How to define custom exception class in Java, the easiest way?

I’m trying to define my own exception class the easiest way, and this is what I’m getting:

public class MyException extends Exception {}

public class Foo {
  public bar() throws MyException {
    throw new MyException("try again please");
  }
}

This is what Java compiler says:

cannot find symbol: constructor MyException(java.lang.String)

I had a feeling that this constructor has to be inherited from java.lang.Exception, isn’t it?

8 Answers
8

Leave a Comment