Site icon TestingDocs.com

User-Defined Exceptions in Java

Overview

When we define or own exceptions in Java, they are called user-defined exceptions or custom exceptions. In this tutorial, we will see how to create your own exception and throw it in Java code. We can throw custom exceptions using the throw keyword.

Product class

In this example, we will take the Product class with some rules.

The following rules are defined for valid products:

1. Product name cannot be null.
2. Price and quantity should be positive.

User-Defined Exceptions

User defined exception class should extend Exception class or any class that extends it.

public class InvalidProductException extends Exception

 

//InvalidProductException class
public class InvalidProductException extends Exception {
private static final long serialVersionUID = 1L;
private String message;

/**
* @param message
*/
public InvalidProductException(String message) {
this.message = message;
}


@Override
public String toString() {
return "InvalidProductException [message=" + message + "]";
}
}

 

Valid Product rules:

You can see in the code that we made to throw InvalidProductException when the rules are violated or not met. Later in the post, we will wrap a driver code to create an invalid product and see that this exception.

throw new InvalidProductException(“Product is invalid!”);

 

if(name==null || price <= 0 || quantity <= 0)
{
try {
throw new InvalidProductException(“Product is invalid!”);
} catch (InvalidProductException e) {
e.printStackTrace();
}
}

 

//Product class

public class Product {
private String name;
private double price;
private int quantity;

/**
* @param name
* @param price
* @param quantity
*/
public Product(String name, double price, int quantity) {
//product rules
if(name==null || price <= 0 || quantity <= 0)
{
try {
throw new InvalidProductException("Product is invalid!");
} catch (InvalidProductException e) {
e.printStackTrace();
}
}
this.name = name;
this.price = price;
this.quantity = quantity;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the price
*/
public double getPrice() {
return price;
}

/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}

/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}

/**
* @param quantity the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}

@Override
public String toString() {
return "Product [name=" + name + ", price=" + price + ", 
quantity=" + quantity + "]";
}
}

Driver class

Driver class to show that invalid product exception is throw, when we try to create an invalid product with negative product price.

//Driver class
public class Driver {

public static void main(String[] args) {
// TODO Auto-generated method stub
Product p =new Product("Car",-4500,100);
p.toString();
}

}

 

 

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version