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

Week 6 Java Lab Programs

The document contains three Java programs demonstrating exception handling. The first program generates an ArrayIndexOutOfBoundsException and handles it with a catch statement. The second program illustrates try..catch..finally, while the third program creates a bank account system that enforces a minimum balance and throws a LessBalanceException for invalid withdrawals.

Uploaded by

harshsing0009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 6 Java Lab Programs

The document contains three Java programs demonstrating exception handling. The first program generates an ArrayIndexOutOfBoundsException and handles it with a catch statement. The second program illustrates try..catch..finally, while the third program creates a bank account system that enforces a minimum balance and throws a LessBalanceException for invalid withdrawals.

Uploaded by

harshsing0009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1. Write a Java program to generate an ArrayIndexOutofBoundsException and handle it using catch statement?

Program:
import java.util.Scanner;
public class Q1 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
System.out.println("The Array is:");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the index to access the array: ");
try {
int index = sc.nextInt();
sc.close();
System.out.println(arr[index] + " is at index " + index + " of the array");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
System.out.println("Rest of the Code");
}
}
Q2. Write a Java program to illustrate try..catch..finally?
Program:
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
try{
System.out.println("This program will print the result of a/b");
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of a: ");
int data1 = sc.nextInt();
System.out.print("Enter value of b: ");
int data2 = sc.nextInt();
sc.close();
System.out.println(data1/data2);
}
catch(ArithmeticException e){ //here if any the exception is not handled
System.out.println(e); //then the code after finally block wont run
}
//catch(NullPointerException e){
// System.out.println(e);
//}
finally{
System.out.println("Inside finally block");
}
System.out.println("Rest of the Code");
}
}
Q3. Write a java program to create Account with 500 rupee minimum balance, deposit amount, withdraw amount
and also throws LessBalanceException which returns the statement that says withdraw amount is not valid?
Program:
import java.util.Scanner;
class LessBalanceException extends Exception{
private static final long serialVersionUID = 1L;
LessBalanceException(String s) {
super(s);
}
}
class Account{
double balance;
public void checkBalance(){
System.out.println("Available Balance is: Rs. "+balance+"\n");
}
public void deposit(double deposit_money){
balance=balance+deposit_money;
System.out.println("Rs. "+deposit_money+" has been successfully deposited into your Account");
System.out.println("Available Balance is: Rs. "+balance+"\n");
}
public void withdraw(double withdraw_money) throws LessBalanceException{
if(balance-withdraw_money<500)
throw new LessBalanceException("Withdraw Amount is not Valid\n");
else{
balance=balance-withdraw_money;
System.out.println("Rs. "+withdraw_money+" has been successfully withdrawn from your Account");
System.out.println("Available Balance is: Rs. "+balance+"\n");
}
}
}
public class Q3 {
public static void main(String[] args) {
Account A1 = new Account();
System.out.print("_________________________\n| |\n");
System.out.print("| WELCOME TO SBI BANK |\n");
System.out.print("|________________________|\n\n");
Scanner sc = new Scanner(System.in);
boolean flag=true;
while(flag){
System.out.println("Press 1 to Deposit Money into your Account");
System.out.println("Press 2 to Withdraw Money from your Account");
System.out.println("Press 3 to Check Available Balance of your Account");
System.out.println("Press 4 to Exit");
System.out.print("\nEnter your chocie: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter how much money you want to deposit: ");
double deposit_money = sc.nextDouble();
A1.deposit(deposit_money);
break;
case 2:
System.out.print("Enter how much money you want to withdraw: ");
double withdraw_money = sc.nextDouble();
try{
A1.withdraw(withdraw_money);
}
catch(LessBalanceException e){
System.out.println("Exception Occured: "+e);
}
break;
case 3:
A1.checkBalance();
break;
case 4:
System.out.println("Thank you for using our Service...\n");
flag=false;
break;
default:
System.out.println("Wrong Chocie!! Please Enter again\n");
}
}
sc.close();
}
}

You might also like