Vrushali Jani JAVA Practice (11.10.21)
Vrushali Jani JAVA Practice (11.10.21)
Vrushali Jani JAVA Practice (11.10.21)
import java.util.Scanner;
public class try_multipleCatch
{
public static void main(String args[])
{
Scanner scn = new Scanner(System.in);
try
{
int n = Integer.parseInt(scn.nextLine());
if (99%n == 0)
System.out.println(n + " is a factor of 99");
}
catch (ArithmeticException ex)
{
System.out.println("Arithmetic " + ex);
}
catch (NumberFormatException ex)
{
System.out.println("Number Format Exception " + ex);
}
}
}
Try-Catch :
public class try_catch {
}
Try-catch-finally
import java.util.Scanner;
class TryCatchFinally {
static void splitString(String text){
try{
String[] splittedString =text.split("/");
for(int i = 0; i < splittedString.length; i++){
System.out.println("Splitted string array of index "+i+" is :
"+splittedString[i]);
}
}
catch(Exception e){
System.out.println("Exception while performing division :
"+e.toString());
}
finally{
System.out.println("I am inside finally block");
}
}
}
Nested Try-catch:
class nested_tryCatch{
public static void main(String args[]){
//main try-block
try{
//try-block2
try{
//try-block3
try{
int arr[]= {1,2,3,4};
/* I'm trying to display the value of an element which doesn't exist.
The code should throw an exception */
System.out.println(arr[10]);
}catch(ArithmeticException e){
System.out.print("Arithmetic Exception");
System.out.println(" handled in try-block3");
}
}
catch(ArithmeticException e){
System.out.print("Arithmetic Exception");
System.out.println(" handled in try-block2");
}
}
catch(ArithmeticException e3){
System.out.print("Arithmetic Exception");
System.out.println(" handled in main try-block");
}
catch(ArrayIndexOutOfBoundsException e4){
System.out.print("ArrayIndexOutOfBoundsException");
System.out.println(" handled in main try-block");
}
catch(Exception e5){
System.out.print("Exception");
System.out.println(" handled in main try-block");
}
}
}
import java.util.Scanner;
class InvalidAmountException extends Exception {
// instance block
{
// Opening account balance with 1500
// you can take any number
this.balance = 1500;
}
}
class AmericanBank implements ATMCard {
// instance block
{
// Opening account with 2000
this.balance = 2000;
}
class ATM {
public static void main(String[] args)
throws InstantiationException,
IllegalAccessException {
/* Declare variables
* To avoid NullPointerException
* initialize them with their default value
*/
String bankName = null, nextOption = null;
int option = 0;
double amount = 0.0, withdrawalAmount = 0.0;
/* Outer while loop will keep ATM machine always in the active mode Program will
remain always active*/
while(true){
// read input
System.out.print("Enter ATMCard (BankName): ");
bankName = scan.next();
try {
// reading class dynamically at runtime
ATMCard card =
(ATMCard)Class.forName(bankName).newInstance();
start: //label
while(true) { //inner-while loop
System.out.println("\n*****Choose operation*****");
System.out.println(" 1. Deposit");
System.out.println(" 2. Withdraw");
System.out.println(" 3. Balance Enquiry");
System.out.println(" 4. Exit");
option = scan.nextInt();
try {
switch(option){
// deposit operation
case 1:
{
System.out.print("Enter amount to deposit: ");
amount = scan.nextDouble();
card.deposit(amount);
System.out.println("Amount "+ amount +
" deposited");
break;
}
// withdraw operation
case 2:
{
System.out.print("Enter amount to withdraw: ");
amount = scan.nextDouble();
withdrawalAmount = card.withdraw(amount);
System.out.println("Collect "+
withdrawalAmount+" cash");
break;
}
// balance-enquiry operation
case 3:
{
System.out.println("Current Balance: "+
card.balanceEnquiry());
break;
}
// exit operation
case 4:
break start;
default:
System.out.println("Invalid option");
} //switch close
catch (InvalidAmountException e) {
System.out.println(e.getMessage());
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
} // inner-catch close
} //inner-while close
} // outer-try close
catch(ClassNotFoundException e) {
System.out.println("Bank is not available.");
} catch (Exception e) {
System.out.println(e.getMessage());
} // outer-catch close
System.out.println("\n*********************************");
System.out.println(" Thank You :) Visit again");
System.out.println("***********************************\n");
} //outer-while close
} // main-method close
} //class close