MainMenu

Home Core Java Overview Maven Tutorials

Saturday, 14 January 2017

Exception Handling in java with example



For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


Exception Handling in java with example


For Video : CLICK HERE



Error & Exception in java :

->Error is related to the environment in which the application is running.
->An Error can't be recovered as it is fatal in nature

A few examples causing Java Runtime Errors are:
->Using a negative size in an array.
->Conversion of a string into a number.
->Divide an integer by zero.
->Access an element that is out of bounds of an array
->Store a value that is of incompatible data type in an array.
->Cast an instance of base class to one of its derived classes.
->Using a null object to reference an object's member.

Capitalization of keywords.
->Missing brackets in a no-argument message.
->Writing the wrong format for a class method, etcetera.
An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following:
“An exception is called and even occurs during the execution of a program, that disrupts the normal flow of the program's instructions”

It can occur for any of the following reasons:
Due to entering invalid data.
Due to lost of network connection in the middle of communications.
Due to missing a required file.
Let's take some exception

1. ArithmeticException
If we divide by zero, there occurs an ArithmeticException.
int x=12230/0;//ArithmeticException
Example : In below example we have written code in try block which will give exception & this exception will be catched in Catch block :

public class Excep
{
public static void main(String args[])
{
try
{
int x =10;
int y = 0;
int z = x/y;
}
catch(Exception e)
{
System.out.println("Exception is :" +e);
}
}
}
Output : java.lang.ArithmeticException: / by zero

2. NullPointerException
If we provide a null value in any variable and performing some task, that causes an NullPointException.
String str=null;
System.out.println(str.length()); //NullPointException
Example :
public class Excep
{
public static void main(String args[])
{
String str = null;
try
{
int x = str.length();
}
catch(NullPointerException e)
{
System.out.println("your exception is:" +e);
}
}
}
}
Output :
your exception is:java.lang.NullPointerException

3. NumberFormatException
The wrong/different formatting of any value, may cause a NumberFormatException.
String str="xyz";
int x=Integer.parseInt(str); //NumberFormatException
public class Excep
{
public static void main(String args[])
{
String str = "xytz";
try
{
int x = Integer.parseInt(str);
}
catch(NumberFormatException e)
{
System.out.println("Your exception is:" +e);
}
}
}
Output :
Your exception is:java.lang.NumberFormatException: For input string: "xytz".

4. ArrayIndexOutOfBoundsException
If we use a value that is an incorrect index then that causes an ArrayIndexOutOfBoundsException.
int x[]=new int[5];
x[10]=1223; //ArrayIndexOutOfBoundException
Example : In below example we have written code in try block which will give exception & this exception will be catched in Catch block also the code with in finally block will be executed whether exception occur or not:
public class Excep
{
public static void main(String args[])
{
int a[] = new int[2];
try
{
System.out.println("Your exception is :"+a[4]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception thrown :" + e);
}
finally
{
a[0] = 6;
System.out.println("First element value: " +a[0]);
System.out.println("The finally statement is executed");
}
}
}
Output :
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 4
First element value: 6
The finally statement is executed

finally block : finally block of statements that needs to be executed regardless of whether or not an exception occurs within the try block.

Throws and Throw Keywaord in java

1.Throws Clause is used to declare an exception and throw keyword is used to throw an exception explicitily.
2.Throws is used with the method signature although Throw is used within the method.
3.Throws can declare multiple exceptions but Throw can not throw multiple exception.

Example of Throws and Throw keyword in Java



throw keyword:
package csc;
public class Throwme
{
int Age;
public void val(int Age)
{
if (Age<18)
{
throw new ArithmeticException("you are kid");
}
else
{
System.out.println("Your welcome");
}
}
public static void main(String args[])
{
Throwme obj = new Throwme();
obj.val(2);
}
}

Output :
Exception in thread "main" java.lang.ArithmeticException: you are kid


throws keyword:
package csc; import java.io.IOException; import java.util.*; public class Test {
public void run() throws IOException
{
Scanner ob = new Scanner(System.in);
System.out.println("Enter your age");
int Age = ob.nextInt();
if(Age <18)
{
throw new IOException("You are a kid");
}
else
{
System.out.println("you are welcomed");
}
}
}
package csc;
import java.io.IOException;
public class Test2
{
public static void main(String args[])
{
Test obj = new Test();
try
{
obj.run();
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Output: Enter your Age 5 java.io.IOException: You are a kid Again run the program Enter your Age 25 you are welcomed.

Tags : Exception in Java with example, Example of Try Catch in Java, what is exception handling in java, Java exception handling with example, Throw and throws in java with example

No comments:

Post a Comment