public class StringEmptyException extends Exception{ private static final long serialVersionUID = 1L; public StringEmptyException(String errorMessage) { super(errorMessage); /* Uncomment the following code and test this program with e.getMessage() System.out.println("Given String is Empty");*/ } } import java.util.Scanner; public class TestException { public static void main(String[] args) { String name; @SuppressWarnings("resource") Scanner input = new Scanner(System.in); name= input.nextLine(); try { if(name.equals("")) { throw new StringEmptyException("Empty String"); }else { System.out.println("Given String is: "+name); } }catch (StringEmptyException e) { e.printStackTrace(); //e.getMessage(); } } }