Exception Handling in Java
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.
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
Example
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");
}}}
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