Java Exceptions

  1. Simple Exception in Java
  2. Multi Catch in Java
  3. Create your Own Exception in Java
  4. Finally Block in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Simple Exception in Java

Output

Try..Catch

try {
 
	// Protected Mode..
} catch (error) {
 
	// Error Catching Mode..
}

Exception_Ex1.java

public class Exception_Ex1 {
	
	public static void main(String args[]) {
		
		int[] array = {1, 2};
		
		try {
			
			System.out.println("The 10th value of Array is : " + array[10]);
		} 
		catch (Exception e) {
			
			System.out.println("The Error : " + e);
		}
	}
}

Sample Output

The Error : java.lang.ArrayIndexOutOfBoundsException: 10




B. Multi Catch in Java


Try..Catch..Catch..Catch..

try {
 
	// Protected Mode..
} catch (error-1) {
 
	// Error Catching Mode - 1 ..
} catch (error-2) {
 
	// Error Catching Mode - 2 ..
} catch (error-3) {
 
	// Error Catching Mode - 3 ..
}

Exception_Ex2.java

public class Exception_Ex2 {
 
	public static void main (String args[]) {
		
		int array[] = {20,10,30};
		int num1 = 15, num2 = 0;
		int sum = 0;
 
		try
		{
			sum = num1 / num2;
			System.out.println("The result is : " + sum);
 
			for(int i =0; i <10; i++)
			{
				System.out.println("The value of array are" +array[i]);
			}
 
		}
		catch (ArrayIndexOutOfBoundsException e)
		{
			System.out.println("Error 1 : " + e);
		}
 
		catch (ArithmeticException e) 
		{ 
			System.out.println ("Error 2 : " + e); 
		}
	}
}

Sample Output

Error 2 : java.lang.ArithmeticException: / by zero
Change it into try block as below :

try
{
 
	for(int i =0; i <10; i++)
	{
		System.out.println("The value of array are" +array[i]);
	}
	
	sum = num1 / num2;
	System.out.println("The result is : " + sum);
 
}

Sample Output

The value of array are20
The value of array are10
The value of array are30
Error 1 : java.lang.ArrayIndexOutOfBoundsException: 3




C. Create your Own Exception in Java


OwnException.java

public class OwnException extends Exception {
	
	public OwnException(int msg) {
		
		super(String.valueOf(msg));
	}
	
	public OwnException(float msg) {
		
		super(String.valueOf(msg));
	}
	
	public OwnException(char msg) {
		
		super(String.valueOf(msg));
	}
 
	public OwnException(String msg) {
		
		super(msg);
	}
}
 
class MainClass {
 
	public static void intFn() throws OwnException {
		
		System.out.println("Throwing OwnException from intFn()");
		throw new OwnException(10);
 	 }
 
	public static void floatFn() throws OwnException {
		
		System.out.println("Throwing OwnException from floatFn()");
		throw new OwnException((float) 111.111);
	}
	
	public static void charFn() throws OwnException {
		
		System.out.println("Throwing OwnException from charFn()");
		throw new OwnException('A');
	}
	
	public static void StringFn() throws OwnException {
		
		System.out.println("Throwing OwnException from StringFn()");
		throw new OwnException("Java World..");
	}
 
	public static void main(String[] args) {
		
		try {
			intFn();
		} 
		catch (OwnException e) {
			e.printStackTrace();
		}
		
		try {
			floatFn();
		} 
		catch (OwnException e) {
			e.printStackTrace();
		}
		
		try {
			charFn();
		}
		catch (OwnException e) {
			e.printStackTrace();
		}
		
		try {
			StringFn();
		}
		catch (OwnException e) {
			e.printStackTrace();
		}
	}
}

Sample Output

Throwing OwnException from intFn()
Throwing OwnException from floatFn()
Throwing OwnException from charFn()
Throwing OwnException from StringFn()
OwnException: 10
	at MainClass.intFn(OwnException.java:30)
	at MainClass.main(OwnException.java:54)
OwnException: 111.111
	at MainClass.floatFn(OwnException.java:36)
	at MainClass.main(OwnException.java:61)
OwnException: A
	at MainClass.charFn(OwnException.java:42)
	at MainClass.main(OwnException.java:68)
OwnException: Java World..
	at MainClass.StringFn(OwnException.java:48)
	at MainClass.main(OwnException.java:75)




D. Finally Block in Java


FinallyMethod.java

public class FinallyMethod {
	
	public static void main(String args[]) {
		
		try {
			
			System.out.println("The Value :");
			
			for(int i=1; i<=3; i++) {
				
				System.out.println(i);
			}
		} 
		catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			System.out.println("The finally block always executes..");
		}
	}
}

Sample Output

The Value :
1
2
3
The finally block always executes..






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com