Java Unit4 ExceptionHandling
Java Unit4 ExceptionHandling
Errors: we write code to build an application, the application may contain errors. We are
responsible for debugging the application. Debugging is art of finding and removing errors in
a software code.
Types Of errors:
1. Compile time errors
2. Runtime errors
Compile Time Errors: All syntax errors are compile-time errors which are due to the poor
understanding of the programming language. A compiler identifies these errors. Once we fix
such errors program will be compiled successfully.
Ex:
• Missing Semicolons
• Missing Brackets of Classes and methods
• Misspelling of identifiers and keywords
• Incompatible types
• Use of undeclared variables, Etc.
Runtime Errors: If an error occurred during execution time of a program then we call it as
Runtime Error .Though the program compiled successfully it may not execute properly due to
logical errors like division with zero, stack overflow etc.
Most common runtime errors are:
• Dividing integer by zero
• Accessing an element out of bound from array
• Attempting to use negative size of array
• Passing parameters not in valid range for a method
• Trying to illegally change the state of a thread
• Trying to use null references of objects.
Etc.
If we want to execute the remaining code of program we will write try, catch blocks to
handle the exception. Once the exception handling is taken as part of the program remaining
code executes successfully.
https://gvsnarasimha.blogspot.com/ 1
Common Java exceptions are: Exceptions in Java can be classified as
1. Checked exceptions
2. Unchecked exceptions
1.Checked exceptions: The Checked exception occur during the compilation time of a
program.
https://gvsnarasimha.blogspot.com/ 2
Exception Handling: The purpose of exception handling is to detect and report an event which
disrupt the normal flow of execution.
Exception handling can be implemented as follows
1. Find the problem (Hit the Exception)
2. Inform that an error has occurred (Throw the Exception)
3. Receive the error information (Catch the Exception)
4. Take corrective actions (Handle the Exception)
The error handling code basically consists of two segments One to detect and to throw
exception and the other to catch the exceptions and take appropriate action.
Syntax: …………………
try {
statements
}
catch(Exception Type e)
{
statements
}
………………….
………………….
A try block is used to write the code which may lead to the exception in a program and throws
an exception . catch block which is defined by the keyword catch used to handle the exception.
We write catch block immediately after the try block. Catch block contain group of statements
which are used to display the information of exception that has occurred.
https://gvsnarasimha.blogspot.com/ 3
// Java Program that make use of Exception Handling
class Ex{
public static void main(String ar[]){
int a=10,b=0,c;
try{
c = a/b; //Arithmetic exception
}
catch(ArithmeticException e)
{
System.out.println("Division by zero is not possible ");
}
}
}
Output: Division by zero is not possible
Syntax…
………………….
try{
statements
}
catch(Exception Type 1 e){
statements
}
catch(Exception Type 2 e){
statements
}
………………….
https://gvsnarasimha.blogspot.com/ 4
// Java Program to demonstrate use of multiple catch statements
class Ex2{
public static void main(String ar[])
{
int array[] ={10,5},b=5,c ;
try{
c = array[2] / b-array[1];
}
catch(ArithmeticException e)
{
System.out.println("Division by zero ");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException");
}
finally{
System.out.println("Example on multiple catch statements");
}
}
}
Output:
ArrayIndexOutOfBoundsException
Example on multiple catch statements
Finally Block: If we write code in finally block the code is guaranteed to execute. The finally
block will execute whether or not an exception is thrown.We use finally block to write code to
free up resources which are allocated in the beginning (Ex: closing a file, closing database
connectivity etc).
Syntax: try{
try{ ……….
……….. ………..}
……….. catch(….. ){
} ………..
finally{ ……….. }
………..
……….. catch( ….. ){
} ………..
……….. }
..
finally{
……….
………..}
https://gvsnarasimha.blogspot.com/ 5