How to create custom exception in java

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).
Create as below
Create POJO class called
Lets create 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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.