How to create custom exception in java

Previous
Next

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.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog