You are on page 1of 6

LAB 4

Topics: Interface, Exception Handling

1. Write a program that catches the divide-by-zero exception using the try-catch
mechanism. Take a numeric value and perform division by zero. Catch the
ArithmeticException.

import java.util.Scanner ;

class Main
{
public static void main(String args[])
{
try
{
Scanner meer=new Scanner (System.in);
System.out.println("Enter first number");
int a=meer.nextInt();
System.out.println("Enter Second number");
int b=meer.nextInt();
int c=a/b;

System.out.println("The value of a/b is :"+ c);

catch(ArithmeticException e)
{
System.out.println("Arithmatic Exception "+e.getCause());
}
}
}
2. Write a java program using multiple catch blocks. Create a class CatchExercise inside
the try block declare an array a[] and initialize with value a[5] =30/5; . In each catch block
show Arithmetic exception and ArrayIndexOutOfBoundsException.

public class jt_lab4_2


{
public static void main(String args[])
{
try
{
int arr[]=new int [5];
arr[5]=30/5;
}
catch(ArithmeticException e)
{
System.out.println("Arithmatic Exception "+e.getCause());
}
catch(IndexOutOfBoundsException e)
{
System.out.println(" Index out of bound "+e.getCause());
}
catch(Exception e)
{

}
finally{
System.out.println("It's Finally block code");
}
}
}
3. Write a program that demonstrates use of finally block. Observe the output of your
program for different cases as mentioned below.

import java.util.Scanner ;

class Main
{
public static void main(String args[])
{
try
{
Scanner meer=new Scanner (System.in);
System.out.println("Enter first number");
int a=meer.nextInt();
System.out.println("Enter Second number");
int b=meer.nextInt();
int c=a/b;

System.out.println("The value of a/b is :"+ c);


meer.close();
}
catch(NullPointerException e)
{
System.out.println("Null Pointer Exception "+e.getCause());
}
catch(ArithmeticException e)
{
System.out.println("Arithmatic Exception "+e.getCause());
}
finally
{
System.out.println("It's Finally block code");
}
}
}
● Case A: exception does not occur. Perform 25/5 mathematical operation. Catch the
NullPointerException.

● Case B: exception occurs but not handled. Perform 25/0 mathematical operation. Catch
NullPointerException.

● Case C: exception occurs and handled. Perform 25/0 mathematical operation. Catch
ArithmeticException.

4. Create an interface Account with two methods: deposit and withdraw. Create class
SavingsAccount which implements the interface. Write a custom Exception handler for
SavingsAccount to handle the scenarios when the withdrawn amount is larger than the
balance in the account.

import java.util.Scanner ;
interface Account
{
public void deposit(int a);
public void withdraw(int b) throws myException;
;
}

class myException extends Exception


{
public myException(String str)
{
super(str);
}
}
class SavingAccount implements Account
{
int balance;
SavingAccount(int balance)
{
this.balance=balance;
}
public void deposit(int a)
{
balance+=a;
}

public void withdraw (int b) throws myException


{
if(b>balance)
{
throw new myException(" Laking of balance");
}
else
balance-=b;

}
}
public class jt_lab_4_4
{
public static void main(String arg[])
{
try
{
Scanner mee=new Scanner (System.in);

SavingAccount mj=new SavingAccount(5000);


System.out.println("Enter 0 for deposit and 1 for
withdraw");
int choice=mee.nextInt();
if (choice==0)
{
mj.deposit(3000);
}
else
{

mj.withdraw(10000);
}

}
catch(myException e){
System.out.println(e);
}
// finally
// {
// System.out.println("It's Finally block code");
// }
}
}

You might also like