Sessions 22, 23 Exception Handling
Sessions 22, 23 Exception Handling
When an array exceeds to it's size, the ArrayIndexOutOfBoundsException occurs. there may be
other reasons to occur ArrayIndexOutOfBoundsException. Consider the following statements.
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Note:
If an exception occurs at the particular statement in the try block, the rest of the block code
will not execute. So, it is recommended not to keep the code in try block that will not throw an
exception.
Java try block must be followed by either catch or finally block.
Program for without
public class try and catch block.
TryCatchExample1
{
public static void main(String[] args)
{
int data=50/0; //may throw exception
System.out.println("rest of the code");
}
}
Output: Exception in thread "main" java.lang.ArithmeticException: / by zero
As displayed in the above example, the rest of the code is not executed (in such case, the rest of the code statement
is not printed).
There might be 100 lines of code after the exception. If the exception is not handled, all the code below the exception
won't be executed.
Java catch block is used to handle the Exception by declaring the type of exception
within the parameter. The declared exception must be the parent class exception ( i.e.,
Exception) or the generated exception type. However, the good approach is to declare
the generated
The catch blocktype
mustof be
exception.
used after the try block only. You can use multiple catch block
with a single try block.
Solution by exception handling
public class TryCatchExample2
{
public static void main(String[] args)
{
try
{
int data=50/0; //may throw exception
}
//handling the exception
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
}
Output: java.lang.ArithmeticException: / by zero
}
public class TryCatchExample4
{
}
Output: java.lang.ArithmeticException: / by zero
rest of the code
Let's see an example to print a custom message on exception.
}
}
}
Output: Can't divided by zero
Let's see an example to resolve the exception in a catch block.
In Java, we can create our own exceptions that are derived classes of the Exception class.
Creating our own Exception is known as custom exception or user-defined exception.
Basically, Java custom exceptions are used to customize the exception according to user
need.
In order to create custom exception, we need to extend Exception class that belongs to
java.lang package.
// class representing custom exception User Defined Exceptions
class InvalidAgeException extends Exception
{ // main method
public InvalidAgeException (String str) public static void main(String args[])
{ {
// calling the constructor of parent Exception try
super(str); {
} } // calling the method
// class that uses custom exception InvalidAgeException validate(13);
public class TestCustomException1 }
{ catch (InvalidAgeException ex)
// method to check the age {
static void validate (int age) throws System.out.println("Caught the exception");
InvalidAgeException{
if(age < 18){ // printing the message from InvalidAgeException
// throw an object of user defined exception object
throw new InvalidAgeException("age is not valid to System.out.println("Exception occured: " + ex);
vote"); }
}
else { System.out.println("rest of the code...");
System.out.println("welcome to vote"); } }
} }
// class representing custom exception
class MyCustomException extends Exception
{