user defined exceptions in Java - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
User-defined exceptions in Java are used to handle the application-specific error conditions. Here application-specific error conditions are bound to a specific application those are not generic for all the applications.
The guys implemented the Java API, provide us extreme Exception handling mechanism which was generic for all the Java-based applications. In the previous tutorials, we have discussed what is Exceptions and top 10 Exceptions in Java. Now we are going to discuss the importance of user-defined exceptions.
The best scenario, when we talk about the user defined exceptions are banking domain applications. In any banking application, the major modules are deposits and withdrawals. When performing a withdrawal from a bank account, it is required to validate the minimum balance in the account. To validate this requirement Java didn’t provide any specific exception class. Hence to handle this requirement, a user-defined exception MinimumAccountBalance may be created.
The user defined exceptions are generated using the throw statement and handle them as normal exceptions.
If necessary, we can write our own user defined exceptions and throw and handle them. We can create the user defined exceptions by extending the Exception class.
class MinimumAccountBalance extends Exception{

}
 
import java.util.Scanner;

class MinimumAccountBalance extends Exception {

    String message;

    public MinimumAccountBalance(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return message;
    }
}

public class UserDefinedException {

    static double current_balance = 100;

    public static void main(String[] args) throws MinimumAccountBalance {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter amount to withdrawal");
        int n = s.nextInt();
        try {
            if (current_balance < n) {
                throw new MinimumAccountBalance("Insufficient funds ! your Current balance is " + current_balance);
            } else {
                System.out.println("Please Take The Money : " + n);
            }

        } catch (MinimumAccountBalance mab) {
            mab.printStackTrace();
        }
    }

}
Output:
Case I:

Enter amount to withdrawal
200
Insufficient funds ! your Current balance is 100.0

Case II:

Enter amount to withdrawal
90
Please Take The Money : 90
Happy Learning 🙂

User defined sorting with Java 8 Comparator
What is Exception in Java
How do we Handle Exception in Java
Top 10 Exceptions in Java
Checked and Unchecked Exceptions in Java
Catching Multiple Exceptions in Java 7
Writing data to a file in Java
Try with Resources Example in Java
Advantages of Exception Handling
String sorting in Java
Java Program to Find the GCD of Two Numbers
Exception Chaining and Debugging Example
Java Program to Find the characters count in String
Factory Method Pattern in Java
How to Rotate an Array to Left direction based on user input ?

User defined sorting with Java 8 Comparator
What is Exception in Java
How do we Handle Exception in Java
Top 10 Exceptions in Java
Checked and Unchecked Exceptions in Java
Catching Multiple Exceptions in Java 7
Writing data to a file in Java
Try with Resources Example in Java
Advantages of Exception Handling
String sorting in Java
Java Program to Find the GCD of Two Numbers
Exception Chaining and Debugging Example
Java Program to Find the characters count in String
Factory Method Pattern in Java
How to Rotate an Array to Left direction based on user input ?
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String