title: demonstrate custom exceptions inheritance java
body: <p>I have been requested to demonstrate the use of inheritance on custom exceptions. This is what I have been asked:
  <em>create a class called MyException:
  -   Using inheritance, create two exception classes: Exception1 and Exception2;
  -   Each class should have appropriate constructors that when used they provide a relevant message, such as, “exception 1” and “exception 2”, respectively;</em></p>
  
  <p><a href="https://i.stack.imgur.com/JpDbd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JpDbd.png" alt="enter image description here"></a></p>
  
  <p>so I created MyException, which inherits from java.lang.Exception:</p>
  
  <pre><code>public class MyException extends Exception{
  
  //default constructor
  public MyException() {}
  
  //constructor that accepts a message
  public MyException(String message){
      super(message);
  }
  </code></pre>
  
  <p>I then create Exception1 and Exception2, but they are empty </p>
  
  <pre><code>public class Exception1 extends MyException {
  
  }
  
  
  public class Exception2 extends MyException {
  
  }
  </code></pre>
  
  <p>Despite it works, I think my approach is not correct.
  Here is how I test it. </p>
  
  <pre><code>//demonstrate use of custom exceptions
  try{
      if(true) {throw new Exception1();}
  }catch (Exception1 e){
      System.out.println("exception1 thrown");
  }
  
  try{
      if(true) {throw new Exception2();}
  }catch (Exception2 e){
      System.out.println("exception2 thrown");
  }
  </code></pre>
  
  <p>How can I do this correctly? What should I put into the Exception1 and Exception2 classes?</p>