Showing posts with label Exception Handling. Show all posts
Showing posts with label Exception Handling. Show all posts

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.

Points To Remember


  • All the custom Exceptions must implement the Exception class or any of its subclass.
  • There are four types of Constructors that you can use while defining your Custom Exception.

Ex 1 : Basic Custom Exception

In this case we will just created a Custom Exception where we define a Custom Exception and throw it with a message. This message is visible in the stacktrace.
class CustomException extends Exception {

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

}

public class Test {
public static void main(String args[]) {
try {
throw new CustomException("Throwing Custom Exception");
} catch (CustomException e) {
e.printStackTrace();
}
try {
throw new CustomException(" ThrowingAnother Custom Exception");
} catch (CustomException e) {
e.printStackTrace();
}
}

}
The output of the above example is
CustomException: Throwing Custom Exception
at Test.main(Test.java:12)
CustomException: ThrowingAnother Custom Exception
at Test.main(Test.java:17)

Ex 2 : Custom Exception with same Message

Suppose we have a Custom Exception and we know that it will occur only because of a particular reason that we can throw the Exception without the Constructor Parameter like the following.
class CustomException extends Exception {

public CustomException() {
super("Custom Exception has Occured");
}

}

public class Test {
public static void main(String args[]) {
try {
throw new CustomException();
} catch (CustomException e) {
e.printStackTrace();
}
try {
throw new CustomException();
} catch (CustomException e) {
e.printStackTrace();
}
}

}
CustomException: Custom Exception has Occured
at Test.main(Test.java:12)
CustomException: Custom Exception has Occured
at Test.main(Test.java:17)


This is a small example how you can define your own Custom Exceptions in java