Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Introduction of Errors
Java Exceptions
Java Try Block
Java Catch Block
Java Finally Block
Java Throw Keyword
Java Throws Keyword
Java Builtin Exceptions
User Defined Exceptions

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java User Defined Exceptions


Java allows us to make our own exceptions called as User Defined Exceptions.

Java’s built-in exceptions handle most common errors,you will probably want to create your own exception types to handle situations specific to your applications.

How to make user defined exception?

Steps

  1. Define an empty class which extends Exception class.
  2. Define parameterised constructor in it and pass argument to super class.
  3. Write try block which throws Exception of newly created class by using className.
  4. Write catch block for UserDefined class and create object of it.
  5. Print message of class using objectName.getMessage() method (optional).

Example

Program for throwing an IncorrectPasswordException when user enters a wrong password.

import java.lang.Exception //optinal
class IncorrectPasswordException extends Exception // user defined class
{
	public IncorrectPasswordException(String msg) {
	super(msg);
	}
}

public class Test 
{
	public static void main(String[] args) 
	{
		int pass=1122;
		try
		{
			if(pass != 1234)
				throw new IncorrectPasswordException("Wrong password entered");
			else
				System.out.println("Welcome user.");
		}
		catch(IncorrectPasswordException e)
		{
			System.out.println("Error! "+e.getMessage());
		}
	}
}

Output

Error! Wrong password entered

In above example, We have created IncorrectPasswordException class which is having Exception as Parent class. We have passed the msg String in its parameterized constructor for initialization of String. In class Test we have written try block which checks for the exception based on written condition.

If condition is true then compiler will throw an IncorrectPasswordException by passing message and call the matching catch block.


User Defined Exceptions based on type of application

We can throw any exception like these.

Sr.no Exception Application Type
1. IncorrectPinException ATM Application
2. ProcessExecutionFailedException System Application
3. BatteryLowException Mobile Application
4. TransactionFailedException Banking/Online Application
5. EmptyTankException Water Purifier Application
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo