OOP LAB Assignment-07
OOP LAB Assignment-07
Programming Exercises:
1. Write a Java program to handle Array Index out of Bounds error.
public class ArrayIndexTest
{
public static void main(String[ ] args)
{
int[] myNumbers = {1, 2, 3};
//System.out.println(myNumbers[10]);
try
{
System.out.println(myNumbers[10]);
}
catch (Exception e)
{
System.out.println("Something went wrong.");
System.err.println(e);
//java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds
for length 3
System.out.println(e.getMessage()); //Index 10 out of bounds for length
3
}
finally
{
System.out.println("This is the FINALLY block..");
}
}
}
catch (ArithmeticException e)
{
System.out.println("Exception: " + e);
System.out.println("Exception message : " + e.getMessage());
}
finally
{
System.out.println("This is the finally block");
}
}
}
class TestCustomException
{
static void validateAge(int age) throws InvalidAgeException
{
if(age<18)
throw new InvalidAgeException("Age not valid");
else
System.out.println("welcome to vote");
}
finally
{
System.out.println("Program comes to end");
}
}
}
4. Write a Java program to check withdrawal limit using user-defined
exception.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class CashWithdraw
{
static void validateAmount(int amount)
{
if(amount < 500)
{
try
{
throw new LowBalanceException("You can't withdraw if amount is less
than 500");
}
catch (LowBalanceException e1)
{
e1.printStackTrace();
}
}
else if (amount >10000)
{
try
{
throw new WithdrawalLimitException("You can't withdraw if amount is greater
than 10000");
}
catch (WithdrawalLimitException e2)
{
e2.printStackTrace();
}
}
else
System.out.println("Withdrawal permitted..");
}
class OddNumberCheck
{
public static void main(String args[]) throws IOException
{
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any number: ");
int num = Integer.parseInt(reader.readLine());
try
{
if(num%2 != 0)
throw(new OddNumberException()); // Statement 2
else
System.out.println("\n\t" + num + " is an even number");
}
catch(OddNumberException Ex)
{
System.out.println("\n\tError : " + Ex.getMessage());
}
class ValidateEmail
{
static void validateEmailID(String email) throws InvalidEmailException
{
if(email.length()<3)
throw new InvalidEmailException("Email ID not valid... too few characters");
else if(email.indexOf('@')<2)
throw new InvalidEmailException("Email ID not valid... @ not found or improper
position");
else if(email.indexOf('.')<(email.indexOf('@')+2))
throw new InvalidEmailException("Email ID not valid .. @ and . cannot come
together");
else
System.out.println("Email ID validated..");
}
Java exceptions cover almost all the general types of exceptions that may occur in the
programming. However, we sometimes need to create custom exceptions.
In order to create a custom exception, we need to extend the Exception class that belongs to
java.lang package.
Example: We pass the string to the constructor of the superclass- Exception which is
obtained using the “getMessage()” function on the object created.