Vrushali Jani JAVA Practice (11.10.21)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Try with Multiple Catch :

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 {

public static void main(String[] args) {


try {
System.out.println(divide(20, 5));
System.out.println(divide(20, 0));
} catch (ArithmeticException ae) {
System.out.println(ae.getMessage());
}
}

public static double divide(int x, int y) {


return x / y;
}

}
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");
}
}
}

public class try_catch_finally


{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number 1 : ");
String text = scanner.nextLine();
TryCatchFinally tcf=new TryCatchFinally();
if(text.length()>0){
TryCatchFinally.splitString(text);
}else{
TryCatchFinally.splitString(null); //Passing null as input for splitting
}
}
}

Throw and Throws :


class MyAge {
static void checkAge(int age) throws ArithmeticException {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years
old.");
}
else {
System.out.println("Access granted - You are old enough!");
}
}
}
public class throws_age {
public static void main(String[] args) {
MyAge ma=new MyAge();
ma.checkAge(15); // Set age to 15 (which is below 18...)
}
}

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 {

public InvalidAmountException(){ // no-arg constructor


super();
}

public InvalidAmountException(String msg) { // String-arg constructor


super(msg);
}
}
class InsufficientFundsException extends Exception {

public InsufficientFundsException(){ // no-arg constructor


super();
}
public InsufficientFundsException(String msg){ // String-arg constructor
super(msg);
}
}
interface ATMCard {

public void deposit(double amount)


throws InvalidAmountException;

public double withdraw(double amount)


throws InvalidAmountException,
InsufficientFundsException;

public double balanceEnquiry();


}
class CitiBank implements ATMCard{

// balance should not be accessible to everyone


private double balance;

// instance block
{
// Opening account balance with 1500
// you can take any number
this.balance = 1500;
}

// method to perform deposit operation


public void deposit(double amount)
throws InvalidAmountException {

if( amount <= 0) {


throw new InvalidAmountException(
"Invalid amount; amount<=0");
}
balance = balance + amount;

// method to perform withdraw operation


public double withdraw(double amount)
throws InvalidAmountException,
InsufficientFundsException {

if( amount <= 0) {


throw new InvalidAmountException(
"Invalid amount; amount<=0");
}

if( balance < amount) {


throw new InsufficientFundsException(amount +
" not available in your account");
}
balance = balance - amount;
return amount;
}
// method to perform balanceEnquiry operation
public double balanceEnquiry(){
return balance;
}

}
class AmericanBank implements ATMCard {

// balance should not be accessible to everyone


private double balance;

// instance block
{
// Opening account with 2000
this.balance = 2000;
}

// method to perform deposit operation


public void deposit(double amount)
throws InvalidAmountException {

if( amount <= 0) {


throw new InvalidAmountException(
"Invalid amount; amount<=0");
}
balance = balance + amount;

// method to perform withdraw operation


public double withdraw(double amount)
throws InvalidAmountException,
InsufficientFundsException {

if( amount <= 0) {


throw new InvalidAmountException(
"Invalid amount; amount<=0");
}
if( balance < amount) {
throw new InsufficientFundsException(amount +
" not available in your account");
}
balance = balance - amount;
return amount;
}

// method to perform balanceEnquiry operation


public double balanceEnquiry(){
return balance;
}

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;

// create Scanner class object


Scanner scan = new Scanner(System.in);

/* 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

// Ask for next operation


System.out.println("\nDo you want to continue? ");
System.out.print("Enter Y or N: ");
nextOption = scan.next();
if("N".equalsIgnoreCase(nextOption))
break start;
} // inner-try 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

You might also like