How to create Nested Custom Exceptions in Java

Points To Rememeber

When you have to create a nested or a group of similar Exceptions in your application then 
  • First create a ParentException class and this class should either extend Exception class or RuntimeException class. 
  • All the nested Exceptions will extend the ParentException and thus will lie in a hierarchy.

Scenario

There may be times when you want to have your own hierarchy of Custom Exception so that you can manage your application in a better way.

Suppose we have a Quiz application where we want to show questions based on a Topic and display questions

Creating Nested Custom Exceptions 

So we have created three exception classes here.

  1. QuixException
  2. InvalidQuestionIdException
  3. InvalidAnswerException
Here Quiz Exception is the Parent of all the other Exceptions. QuizException can catch all the nested Exceptions so that we do not have to catch each of them separately.  


class QuizException extends Exception {

 public QuizException(String message) {
  super(message);
 }

}

class InvalidQuestionIdException extends QuizException {

 public InvalidQuestionIdException() {
  super("invalid question id provided");
 }

}

class InvalidAnswerException extends QuizException {

 public InvalidAnswerException() {
  super("invalid answer is provided");
 }

}
class Question {
 public String getQuestion(Integer id) throws InvalidQuestionIdException{
  switch (id) {
  case 1:
   return "This is question 1";
  case 2:
   return "This is question 2";
  default:
   throw new InvalidQuestionIdException();
  }
 }

}


class Answer{
 public String getAnswer(Integer ans)throws InvalidAnswerException{
  switch(ans){
  case 1:
   return "right answer";
  case 2:
   return "wrong answer";
  default:
   throw new InvalidAnswerException();
  }
  }
 }
 
 


public class Test {
 public static void main(String args[]) {
  try {
   System.out.println(new Question().getQuestion(1));
   System.out.println(new Question().getQuestion(5));
  } catch (QuizException e) {
   e.printStackTrace();
  }
  try {
   System.out.println(new Answer().getAnswer(1));
   System.out.println(new Answer().getAnswer(3));
  } catch (QuizException e) {
   e.printStackTrace();
  }
 }

}
Output of the above example is as follows
This is question 1
InvalidQuestionIdException: invalid question id provided
at Question.getQuestion(Test.java:32)
at Test.main(Test.java:59)
right answer
InvalidAnswerException: invalid answer is provided
at Answer.getAnswer(Test.java:47)
at Test.main(Test.java:65)
This is how you create Nested Custom Exceptions in java. When you want to create hierarchy of Exception you can keep extending the QuizException or any other Exception that extend Quiz Exception.

No comments:

Powered by Blogger.