TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Java Tutorials

    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/

    Related Posts

    Java Performance

    Java Tutorials /

    Improving Java Performance with Multithreading

    Download Greenfoot Windows

    Java Tutorials /

    Download & Install Greenfoot on Windows

    Java Tutorials /

    Java Static Code Analysis

    Java Tutorials /

    Java Testing Tools

    Java Tutorials /

    Handle Multiple Exceptions in Java

    ‹ Java Classes and Objects› Java Data Types

    Recent Posts

    • Running Tests in Parallel with Selenium Grid
    • Advanced Selenium Features
    • Locating Web Elements
    • Running the Test Script
    • Writing Your First Selenium Test Script
    • Getting Started with Selenium Automation Testing
    • Setting Up the Environment
    • How can you monitor the Quality Assurance Audit?
    • Leveraging LambdaTest with Appium 2.0
    • Appium 2.0 Plugins ListAppium 2.0 Plugins
    • Touch Actions and Multi-Touch Actions
    • Changes in Drivers and Classes
    • Appium Inspector
    • Capabilities in Appium 2.0
    • Appium 2.0 Driver ListAppium 2.0 Driver Installation & Management
    CyberLink Multimedia Software

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com