User Defined exception in Java | Java custom exception examples

In Java programming, it is possible to create our own exceptions and such exception are called user-defined exceptions in Java.

The exception class is a predefined class. Any user-defined exception created by us must be a subclass of the predefined Exception class and created by extending the Exception class.

Read More

  1. Exception Handling in Java: Hierarchy Example and Types
  2. Exception Handling in Java with Examples
  3. Exception Handling try catch finally blocks in Java
  4. Throw and Throws Keywords in Java

What is user defined (custom) exception in java

An Exception that is defined by the user/ programmer is known as a user-defined or custom exception.

example

  1. Checking user age if not valid age throw an exception
  2. If not in a specified time period then throw an exception
  3. If the user name and password are incorrect then throw an exception

All above exceptions are created by the user and throw when required.

How to throw user defined exception in java

 The throw and throws keywords are used for implementing user-defined exceptions. 

throw: throw keyword is used to throw an exception explicitly from a method or form a block.   

for instance

 Here new NumberIsGreaterException();

is creating a new exception and throwing it.

throws: throws keyword is used with a method signature that informs that the method can throw a mentioned exception.   

Here Scanner is class and the above-mentioned Scanner represents the constructor call, On the constructor call of above, we have to handle File not found Exception.

how to create user defined exception in java

  1. Create a class
  2. Extend the Exception class
  3. Create a String argument constructor to pass the custom messages.

Examples of User Defined Exception in Java

Example1:   write a program to take a number from a user by using the command line argument during run time. if the user will enter more than 100 then generate the user-defined exception MyException.

Explanation: In the above program, the name of the user define exception is NumberIsGreaterException. 

In the above program, the user enters the number 111 during run time using a command-line argument.

Here the value of “m” is greater than 100 therefore  “throw newNumberIsGreaterException ();” this throw statement throws the exception and catches by the catch block but before throw “NumberIsGreaterException ()” constructer will call.

User defined exception in java program to check age is valid or not

invalid age exception in java : invalidageexception

InvalidAgeException.java

CheckAge.java

Output

user defined exception in java example to check a time is in range or not, if time is not in range then throw custom exception

InvalidTimeException.java

CheckTime.java

Output

Example: Write a program to create a user-defined exception in Java to check whether a given name exists or not. If the name doesn’t exist then throw a custom exception.

InvalidNameException.java

CheckName.java

Output

write a java program to validate the full name of an employee. create and throw a user defined exception if firstname and lastname is blank.

or

write a java program to validate the full name of an employee using exception

How to write custom exception in java?

or

How to create custom exception in java?

or

How to create your own exception in java?

  1. Create a class
  2. Extend Exception class
  3. Create a constructor to show/pass a custom message

Q: accept name and age in console and if the age is less than 17 or greater than 24 ,throw a user defined exception “invalidageexception” with the age entered.

Go up and see invalid age exception in java