EXCEPTION HANDLING IN JAVA
EXCEPTION:
Exception means an abnormal condition.
It is an event that disrupts the normal flow of the program.
It is an object which is thrown at runtime.
EXCEPTION HANDLING:
It is a mechanism to handle runtime errors.
Exception handling done with the exception object.
Normal flow of the application can be maintained
It is an object which is thrown at runtime.
TYPES OF ERRORS
There are three types of errors.
1. Syntax errors.
2. Run time error
3. Logic errors
TYPES OF EXCEPTION
There are three types of exception
1. Checked exception
2. Unchecked exception
3. Error
1) Checked Exception
The classes that directly inherit the Throwable class except Runtime Exception and
Error are known as checked exceptions. Checked exceptions are checked at compile-time.
Example: IOException, SQLException
2) Unchecked Exception
The classes that inherit the Runtime Exception are known as unchecked exceptions.
Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
Example: ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException
3) Error
Error is irrecoverable.
Example: OutOfMemoryError, VirtualMachineError, AssertionError etc.
EXCEPTION HANDLING KEYWORDS:
1. try
2. catch
3. finally
4. throw
5. throws
I. Try and catch block:
It should be followed immediately by the catch block.
single try block can have multiple catch block.
In multiple catch block "Exception" class should be placed only at the last otherwise
other specified exceptions will be useless.
Finally block can be used only with the try and catch block.
order of these block should be
try
catch
finally
Catch block
It is placed directly after the try block to handle one or more exception types.
Syntax of try - catch block
try
{
//statements
}
catch(Exception object)
{
//statements
}
Example:
public class test
{
public static void main(String[] args)
{
try
{
int data=50/0;
}
catch(ArithmeticException e)
{
System.out.println(" Divide by zero exception");
}
System.out.println("Hello");
}
}
output:
Divide by zero exception
hello
ii) Nested try block:
The try block inside another try block is called as nested try block.
When a part of a block may cause one error and the entire block itself may cause
another error. In such a cases, exception handler have to be nested.
Syntax:
try
{
statement1;
statement2;
// Inner try block
try
{
statement1;
statement2;
}
catch(Exception ex)
{
//code
}
}
catch(Exception ex)
{
//code
}
Example:
public class test {
public static void main(String args[]) {
try
{
try
{
int data= 10/0;
}
catch(Exception e)
{
System.out.println("ArithmeticException");
}
try
{
int[] arr = new int[6];
arr[7] = 8;
}
catch(Exception e)
{
System.out.println("ArrayIndexOutOfBoundsException");
}
System.out.println("hi");
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Hello");
}
}
Output:
ArithmeticException
ArrayIndexOutOfBoundsException
hi
Hello
iii) Finally block
It is used to execute important code such as closing connection, stream etc.,
Finally block is always executed whether exception is handled or not.
Finally block follows try or catch block.
Syntax:
try
{
//Statements
}
catch
{
//Handling exception
}
finally
{
//Statements to be executed
}
Example
public class test
{
public static void main(String[] args)
{
try
{
int data =30/0;
}
catch(Exception e)
{
System.out.println("Arithmetic Exception");
}
finally
{
System.out.println("finally block");
}
System.out.println("Hello Java");
}
}
Output:
Arithmetic Exception
finally block
Hello Java
iv) Throw
The throw keyword is used to thrown an exception.
It is used to generate an exception or describe an instance of an exception.
Syntax:
catch(ExceptionType object)
{
throw object;
}
Syntax:
throw new ExceptionType(args);
Example:
public class test
{
public static void main(String[] args)
{
int balance=100,withdraw=1000;
if(balance<withdraw)
{
throw new ArithmeticException("No money");
}
else
{
System.out.println("withdraw");
}}}
Output: Exception in thread "main" java.lang.ArithmeticException: No money
v)Throws
The throws keyword is used to declare an exception.
It doesn't throw an exception.
Syntax:
method_name(parameter_list) throws Exception_list
Example:
public class test
{
static void checkAge(int age) throws ArithmeticException
{
if (age < 18)
{
throw new ArithmeticException("Not eligible for voting");
}
else
{
System.out.println("Eligible for voting");
}
}
public static void main(String[] args)
{
checkAge(15);
}
}
Output:
java.lang.ArithmeticException: Not eligible for voting
Advantages of Exception handling:
1. Provision to complete program execution
2. Easy identification of program code and error handling code
3. Propagation of errors
4. Meaningful error reporting
5. Identifying error types