Friday , July 21 2017
Home / java / user defined exceptions in Java

user defined exceptions in Java

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 were generic for all the Java based applications. In the previous tutorials we have discussed about what is Exceptions and top 10 Exceptions in Java. Now we are going to discuss importance of user defined exceptions.

User Defined Exceptions Scenario

The best scenario, when we talk about the user defined exceptions are banking domain applications. In any banking application, the major module 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 a 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.

Syntax:


class MinimumAccountBalance extends Exception{

}

Note: “user defined exceptions class names does not not match with already existed Exception classes”.

 

User Defined Exceptions Example


 


import java.util.Scanner;

/**
 *
 * @author chandrashekhar
 */
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

 You may also Like

What is Exception

How to handle Exceptions in Java

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Binary Literals Java 7 Example Tutorials

Binary literals is one of the new feature added in Java 7. We already know …

Leave a Reply

Your email address will not be published. Required fields are marked *