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 Exception Handling


Exception Handling in Java

What is Exception?

An Exception is a condition that is caused by a run-time error in a program. We can also say that an Exception is an event which disturbs the normal flow of the program.

The Exception handling mechanism actually detects and reports an "Exception circumstance" so that the corrective action will be taken.

Different tasks of error handling.

Sr.no Task Action
1. Finding the problem in code Hit the exception
2. Notify that an error has occurred. Throw the exception
3. Collect the error information Catch the exception
4. Take the proper action Handling the exception

How exception handling is performed?

Java exception handling is performed by following 5 keywords.

  1. try
  2. catch
  3. throw
  4. throws
  5. finally

1. try statement

All program statements which are going to generate an exception are written inside the try block.

If an exception is found in the try block then it is thrown to particular matching catch block for handling purpose.

If any one statement generates the exception in try block then all other remaining statements are skipped and execution is jumped into the matching catch block.

Example

try
{
    int a=25, b=0;
    int c = a/b; // generates an exception as Divided by zero.
    System.out.println("Division is : "+c); // compiler skipes this line and jumps into catch block.
}

2. Catch statement

Catch block is responsible for handling an exception which is generated in the try block. It also contains multiple statements to take corrective action on the exception.

Every try block must have atleast 1 catch block, otherwise compiler gives an error.

Example

try
{
    int a=25, b=0;
    int c = a/b; // generates an exception as Divided by zero.
    System.out.println("Division is : "+c); // executed if there is no Exception.
}
catch(ArithmeticException AE)
{
    System.out.println("Divided by zero error\nPlease change inputs");
    // take action if error occurred. But never goes to try block.
}

3. throw

Throw keyword is used for manually (explicitly) throwing an exception.

Since throw is used for manually throwing an exception it is used within the method.

It doesn't allows us to throw multiple exceptions at a time. We have to go for the throws keyword.

Example

try
{
    throw new NullPointerException ("Null Exception");
}
catch(NullPointerException e)
{
    System.out.println("Null pointer exception caught");
}

4. throws statement

Throws keyword allows us to execute multiple exceptions from a method.

If a method is generating an exception bu cannot handle it, then it is the job of method to specify the task to protect them from an Exception.

The exception that can be generated by method must be thrown by using throws clause otherwise the compile time error will be generated.

Example

class ThrowsTest
{
   
    static void throwsMethod() throws ArithmeticException
    {
        int a = 10, b = 0 , c;
	    c = a/b;
	    throw new ArithmeticException ("Error");
    }		
	public static void main(String args[]) 
	{
		try
		{
		throwsMethod();
		}
		catch(ArithmeticException e)
		{
			System.out.println("Divided by zero error");
		}
	}
}

5. finally statement

If the exception is not caught by any catch block then finally block is responsible for handling the exception.

finally block statements are always executed after the try catch block has completed its execution.

Rules for writting finally block

Example

try
{
    int a=25, b=0;
    int c = a/b; // generates an exception as Divided by zero.
    System.out.println("Division is : "+c); // executed if there is no Exception.
}
catch(ArithmeticException AE)
{
    System.out.println("Divided by zero error. Please change inputs");
    // take action if error occurred. But never goes to try block.
}
finally
{
    System.out.println("Finally block executed");
}
<< 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