Friday, November 4, 2016

Java Exception Handling Programs

A Java Exception is an object that describes the exceptional condition that has occurred in a piece of code. These Exceptions handling is managed by try,catch,throw,throws and finally. In this article,we will learn some exception handling java programs which are frequently asked in the technical written Test  and interview room also. Exception means runtime errors,it is an unexpected unwanted event which disturbs entire flow of the program. If we are not handling exception,the program may terminate abnormally without releasing allocated resource. You must notice one thing about Exception concept, Exception handling means we are not repairing exception we are providing alternative way to continue the program normally. Also read  java exception interview questions in my previous post. Now let's look at exception handling programs one by one.



1) Java Program for NullPointerException:

It is the child class or Runtime Exception and it is unchecked,thrown automatically by the JVM whenever we are performing any operation on null.

Program:

class Exception1
{
public static void main(String args[])
{
try{
String s=null;
System.out.println(s.length());
}
catch(NullPointerException e)
{
System.out.println("NullPointerException");

}
}

Output:




Program 2: ArrayIndexOutOfBoundsException

class Exception2
{
public static void main(String args[])
{
try{
int arr[]=new int[10];//here array has 10 elements only
arr[11]=8;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException");
}
}
}

Output:




Program 3: StringIndexOutOfBoundException

class StringException
{
public static void main(String args[])
{
try{
String s="hello lucky";
System.out.println(s.length());
char c=s.charAt(0);
c=s.charAt(30);
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("StringIndexOutOfBoundException");
}
}
}

Output:




Program 4: NumberFormatException

class NumberFormat
{
public static void main(String args[])
{
try{
int num=Integer.parseInt("abc");
System.out.println(num);
}
catch(NumberFormatException e)
{
System.out.println("NumberFormatException");
}
}
}

Output:




Program 5:  How to throw an already defined exception using throw keyword

class Exception5
{
static int sum(int num1, int num2)
{
if(num1==0)
throw new ArithmeticException("First parameter is not valid");
else
System.out.println("both parameters are correct");
return num1+num2;
}
public static void main(String args[])
{
int res=sum(0,16);
System.out.println(res);
}
}
Output:




Program 6: How to throw your own Exception explicitly using throw

class MyOwnException extends Exception
{
public MyOwnException(String msg)
{
super(msg);
}
}
class Exception6
{
static void employeeAge(int age) 
throws MyOwnException
{
if(age<0)
throw new MyOwnException("Age can not be less than zero");
else
System.out.println("input is valid");
}
public static void main(String args[])
{
try
{
employeeAge(-3);
}
catch(MyOwnException e)
e.printStackTrace();
}
}
}

Output:




Program 7 : Multiple catch Blocks

class Exception7
{
public static void main(String args[])
{
try{
int a[]=new int[9];
a[4]=20/0;
System.out.println("First print statement in try block");
}
catch(ArithmeticException e)
{
System.out.println("Warning ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Warning ArrayIndexOutOfBoundsException");
}
catch(Exception e)
{
System.out.println("Warning some other exception");
}
System.out.println("out of try-catch block");
}
}

Output:




Program 8: How to handle run time exceptions

class NeverCaught
{
static void m()
{
throw new RuntimeException("from m()");
}
static void n()
{
m();
}
public static void main(String args[])
{
n();
}
}

Output:




Program 9: Java finally block

class FinallyDemo
{
public static void main(String args[])
{
System.out.println(FinallyDemo.m());
}
public static int m()
{
try
{
return 99;
}
fianlly
{
System.out.println("this is finally block");
System.out.println("finally block ran even after return statement");
}
}
}


Output:




Program 10: Nested try block
class NestedTry
{
public static void main(String args[])
{
//parent try block
try{
//child try block1
try{
System.out.println("inside block1");
int a=55/0;
System.out.println(a);
}
catch(ArithmeticException e1)
{
System.out.println("Excepiton: e1");
}
//child try block2
try
{
System.out.println("inside block2");

int a=55/0;
System.out.println(a);
}
catch(ArrayIndexOutOfBoundsException e2)
{
System.out.println("Excepiton: e2");
}
System.out.println("other statement");
}
catch(ArithmeticException e3)
{
System.out.println("ArithmeticException");
System.out.println("inside parent try catch block");
}
catch(ArrayIndexOutOfBoundsException e4)
{
System.out.println("ArrayIndexOutOfBoundsExcepton");
System.out.println("insdie parent try catch block");
}
catch(Exception e5)
{
System.out.println("Exception");
System.out.println("inside parent try catch block");
}
System.out.println("next statement");
}
}

Output:









No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...