Try, Catch and Finally in Java

Advertisement:

This Java Video Tutorial illustrates the use of try, catch and finally along with the Exceptions like: ArrayIndexOutOfBoundsException, ArithmeticException.

Exceptions:
Checked Exceptions: Environmental error that cannot necessarily be detected by testing; e.g. disk full, broken socket, database unavailable, etc.
Errors: Virtual machine error: class not found, out of memory, no such method, illegal access to private field, etc.
Runtime Exceptions: Programming errors that should be detected in testing: index out of bounds, null pointer, illegal argument, etc.

Checked exceptions must be handled at compile time. Runtime exceptions do not need to be. Errors often cannot be.

Why to use Exceptions:
To handle the situations where the code acts the way you did not intend it to act.
For example, if the user tries to divide a number by zero: user keyboard input.

In such a situation, we need to handle the code to make the program work with stability.

Note:
There can be only one try block and any number of catch blocks can be written.

Array declaration:

int[] array = new int[3];

array is the variable name.

Try block which generates ArrayIndexOutOfBoundsException Exception:

	try
		{
			for(int i=0; i<4; i++)
			{
			array[i] = i;
			System.out.println(array[i]);
			}
		}

Notice that we have array[0], array[1], array[2] but we are trying to print even array[3] which isn’t present.
Notice the for loop, it executes 4 times, instead of 3 times, so raising exception.

Try block which generates ArithmeticException Exception:

		try
		{
			for(int i=0; i<3; i++)
			{
			array[i] = i;
			System.out.println(array[i]);
			}
 
			array[0] = 2/0;
		}

Notice the division by zero.
We purposefully assign 2/0 to array[0] to raise ArithematicException.

Super Class: Exception

catch(Exception e)
{
	System.out.println("You did some mistake, correct yourself!");
}

those exceptions which are not caught by those specific Exception handlers, those and any other unknown or unexpected exception are handled by Exception class.

Finally:

finally
{
	System.out.println("I get executed each time irrespective of the situation!");
}

the code inside finally block gets executed irrespective of the exception being raised or not.
That is, the code inside finally block will always be executed, if the program doesn’t crash!

Video Tutorial: Try, Catch and Finally in Java


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=_j99G6u206o [Watch the Video In Full Screen.]




Full Source Code:(execute.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.lang.*;
 
class exception
{
 public static void main(String args[])
 {
	int[] array = new int[3];
 
	try
	{
		for(int i=0; i<3; i++)
		{
		array[i] = i;
		System.out.println(array[i]);
		}
 
	//	array[0] = 2/0;
	}
	catch(ArrayIndexOutOfBoundsException e)
	{
		System.out.println("Do not try to print more than it can store!");
	}
	catch(ArithmeticException e)
	{
		System.out.println("Do not divide by zero..learn math!");
	}
	catch(Exception e)
	{
		System.out.println("You did some mistake, correct yourself!");
	}
 
	finally
	{
	System.out.println("I get executed each time irrespective of the situation!");
	}
 
 }
}

Output:
0
1
2
I get executed each time irrespective of the situation!

Advertisement:

Enter your email address:

RSS Feed




Leave a Reply