In this post, we will see how to create custom exception in java. It is very simple to create custom exception in java. You just need to extends Exception class to create custom exception.
Lets understand this with example. You have list of counties and if You have “USA” in list of country, then you need to throw invalidCountryException(Our custom exception).
Example:
Create InvalidCountryException.java as below
Create POJO class called Country.java
Lets create CountryCheckMain.java. This class will have main method.
When you run above program, you will get following output:
As you can see, if we have “USA” in list of Countries,we are throwing InvalidCountryException.
Lets understand this with example. You have list of counties and if You have “USA” in list of country, then you need to throw invalidCountryException(Our custom exception).
Example:
Create InvalidCountryException.java as below
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.