Search code examples
javaexceptioncustom-exceptions

Custom Exceptions Class Naming in Java


I am trying to create a custom exception class but having trouble naming. Here's what I have so far:

public class MyException extends Exception {
     public MyException() {
     }
}

Now, I want an exception that is not called MyException. Can I use:

public void EmptyException() {
}

Thank you for your help.


Solution

  • yes ofcourse, just do the same thing again :) create a new class called EmptyException

    in same class:

        public class EmptyException extends Exception {
             public EmptyException() {
             }
            public class InnerException extends Exception { //Inner class
               public InnerException() {
                   }
    
              }
        }