0% found this document useful (0 votes)
60 views

Ce127 - JT - Lab - 4

The document contains instructions for 4 programming exercises related to exception handling in Java: 1. Write a program to catch a divide-by-zero exception using try-catch and demonstrate catching ArithmeticException. 2. Write a program using multiple catch blocks to catch ArithmeticException and ArrayIndexOutOfBoundsException. 3. Write a program demonstrating the use of the finally block and observe the output for different exception cases. 4. Create an Account interface with deposit and withdraw methods and SavingsAccount class implementing it. Write a custom exception handler for withdrawals exceeding the account balance.

Uploaded by

CE127 MEET SHAH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Ce127 - JT - Lab - 4

The document contains instructions for 4 programming exercises related to exception handling in Java: 1. Write a program to catch a divide-by-zero exception using try-catch and demonstrate catching ArithmeticException. 2. Write a program using multiple catch blocks to catch ArithmeticException and ArrayIndexOutOfBoundsException. 3. Write a program demonstrating the use of the finally block and observe the output for different exception cases. 4. Create an Account interface with deposit and withdraw methods and SavingsAccount class implementing it. Write a custom exception handler for withdrawals exceeding the account balance.

Uploaded by

CE127 MEET SHAH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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