0% found this document useful (0 votes)
52 views8 pages

WT Lab 10

The document defines classes to throw custom exceptions for invalid time input, insufficient command line arguments, and low bank balance withdrawal. It demonstrates catching and handling the exceptions in sample programs that get user input for time, parse command line arguments, and perform bank transactions with exception handling.

Uploaded by

ankit
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)
52 views8 pages

WT Lab 10

The document defines classes to throw custom exceptions for invalid time input, insufficient command line arguments, and low bank balance withdrawal. It demonstrates catching and handling the exceptions in sample programs that get user input for time, parse command line arguments, and perform bank transactions with exception handling.

Uploaded by

ankit
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/ 8

Q10.

5>

import java.util.*;

class SecException extends Exception

SecException(String s)

super(s);

class MinException extends Exception

MinException(String s)

super(s);

class HrsException extends Exception

HrsException(String s)

super(s);

}
}

class five

static void checkTime(int h,int m, int s) throws HrsException,MinException,SecException

if(h>24 || h<0)

throw new HrsException("Invalid input for hour");

else if(m>60 || m<0)

throw new MinException("Invalid input for minute");

else if(s>60 || s<0)

throw new SecException("Invalid input for seconds");

else

System.out.println("Correct Time -> "+h+" : "+m+" : "+s);

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print("Enter hours : ");

int h=sc.nextInt();

System.out.print("Enter minutes : ");

int m=sc.nextInt();

System.out.print("Enter seconds : ");

int s=sc.nextInt();

try

{
checkTime(h,m,s);

catch(Exception e)

System.out.println(e);

sc.close();

}
10.6 >

class CheckArgs extends Exception

CheckArgs(String s)

super(s);

class six

static void arg() throws CheckArgs

throw new CheckArgs("Input arguements less than 4");

public static void main(String[] args)

if(args.length!=4)

try

arg();

catch (Exception e)
{

System.out.println(e);

finally

System.exit(0);

int a[] = new int[4];

for(int i=0;i<4;i++)

a[i] = Integer.parseInt(args[i]);

System.out.print("Argumets not less than 4 so squares of numbers = ");

for(int i=0;i<4;i++)

System.out.print((a[i]*a[i])+" ");

}
10.7>

import java.util.*;

class LessBalance extends Exception

LessBalance()

System.out.println("Minimum Balance should be 500");

LessBalance(String s)

super(s);

class Bank

final int min = 500 ;

int bal = min ;

int show()

return bal;

void deposit(int amt)

bal = bal + amt;


}

void withdraw(int amt) throws LessBalance

if ( bal - amt > min)

bal = bal-amt;

else

throw new LessBalance();

class seven

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

Bank obj = new Bank();

System.out.println("1. Deposit\n2. Withdraw\n3. Show\n4. Exit");

try

while(true)

System.out.print("Enter your choice : ");

int ch=sc.nextInt(),amt;

switch(ch)

case 1:
System.out.print("Enter amount to deposit : ");

amt = sc.nextInt();

obj.deposit(amt);

break;

case 2:

System.out.print("Enter amount to withdraw : ");

amt = sc.nextInt();

obj.withdraw(amt);

break;

case 3:

System.out.println("The current balance is = "+obj.bal);

break;

case 4:

System.exit(0);

default :

System.out.println("Invalod operation, Please choose a valid operation");

catch (LessBalance e)

System.out.println(e+" Exception Occured");

You might also like