Google News
logo
Java Exception Handling
Exception Handling
An exception is a problem occurred during execution time of the program.
An unwanted unexpected event that disturbs normal flow of execution called exception.
Exception is nothing but a object.
Exception is a class present in java.lang package.
All the exceptions are nothing but objects called classes.
Whenever user is entered invalid data then Exception is occur.
A file that needs to be opened can't found then Exception is occurred.
Exception is occurred when the network has disconnected at the middle of the communication.

Types of Exceptions:
Java Images
The Exceptions are divided into three types
1) Checked Exception
2) Unchecked Exception
3) Error 

checkedException :

The Exceptions which are checked by the compiler at compilation time for the proper execution of the program at runtime is called CheckedExceptions.
Ex: IOException,SQLException etc.
Error:-
Errors are caused by lack of system resources . these are non recoverable.
Ex: StackOverFlowError,OutOfMemoryError,AssertionError etc.
Exceptions:-
There are two types of exceptions present in the java language
1) Predefined Exceptions.
2) User defined Exceptions.

Predefined Exception :
Predefined classes comes along with the software based on your requirement we have to create a objects.
Ex: ArithmeticException,IOException,NullPointerException..etc 

User defined Exceptions : 
Based on the user requirement user can creates a Exception is called user defined Exception. 
Ex: InvaliedAgeException,BombBlostException..etc 

To create user defined Exceptions: 

1) To create user defined exception we have to take an user defined class that is a sub class to the RuntimeException(for creation of unchecked Exceptions) . 

2) To create user defined exception we have to take user defined class that is subclass to the Exception(for creation of checked Exceptions) 

3) Each and every Exception contains two constructors
a) default constructor
b) parameterized constructor
ArrayIndexOutOfBoundsException:-
class Test 
{ 
public static void main(String[] args) 
{ 
try 
{ 
int[] a={10,20,30}; 
System.out.println(a[0]);//10 
System.out.println(a[1]);//20 
System.out.println(a[2]);//30 
System.out.println(a[3]);//ArrayIndexOutOfBoundsException
} 
catch (ArrayIndexOutOfBoundsException ae) 
{ 
System.out.println("boss u r geting ArrayIndexOutOfBoundsException"); 
System.out.println("check u r code once"); 
} 
} 
}
Output :
output: /ArrayIndexOutOfBoundsException
NumberFormatException:-
class Test 
{ 
public static void main(String[] args) 
{ 
try 
{ 
String str="123"; 
int a=Integer.parseInt(str); 
System.out.println(a);//conversion(string - int) is good
String str1="abc"; 
int b=Integer.parseInt(str1); 
System.out.println(b);//NumberFormatException 
} 
catch (NumberFormatException ae) 
{ 
System.out.println("boss u r geting NumberFormatException"); 
System.out.println("check once u r code"); 
} 
} 
}
Output :
output:   NumberFormatException
NullPointerException:-
class Test 
{ 
public static void main(String[] args) 
{ 
try 
{ 
String str="venkat"; 
System.out.println(str.length());
String str1=null; 
System.out.println(str1.length());//NullPointerException 
} 
catch (NullPointerException ne) 
{ 

} 
} 
}
Output :
output:6
NullPointerException
ArithmeticException:-
class Test 
{ 
public static void main(String[] args) 
{ 
try 
{ 
int a=10/2; 
System.out.println(a);//5
int b=10/0; 
System.out.println(b);//ArithmeticExceptiom 
} 
catch (ArithmeticException ne) 
{ 
System.out.println("boss u r geting ArithmeticException"); 
System.out.println("check once u r code"); 
} 
} 
}
Output :
output:boss u r geting ArithmeticException
check once u r code
NegativeArraySizeException:-
class Test 
{ 
public static void main(String[] args) 
{ 
int[] a1=new int[100]; 
System.out.println(a1.length);//100 
int[] a=new int[-9]; 
System.out.println(a.length);//NegativeArraySizeException 
} 
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: at ee.Test.main(Test.java:7)
inputMismatchException
import java.util.*; 
class Test 
{ 
public static void main(String[] args) 
{ 
Scanner s=new Scanner(System.in); 
System.out.println("enter first number"); 
int a=s.nextInt(); 
System.out.println("enter second number"); 
int b=s.nextInt(); 
System.out.println(a+b); 
} 
}
Output :
output:enter first number
venkat

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at ee.Test.main(Test.java:12)