[Link].
: 07
Date : 21/10/2020
Exceptions
PROGRAM 1:
Aim:
To write a program to implement user defined exception if age is invalid.
Source Code:
import [Link].*;
class AgeException extends Exception {
public AgeException(String str) {
[Link](str);
}
}
public class Age{
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
[Link]("Enter your age :: ");
int age = [Link]();
try {
if(age < 18)
throw new AgeException("Invalid age");
else
[Link]("Valid age");
}
catch (AgeException a) {
[Link](a);
}
}
}
Output:
19I433 [Link] Page 1
[Link]. : 07
Date : 21/10/2020
Result:
Thus to write a program to implement AgeInvalidException was successfully
executed.
PROGRAM 2:
Aim:
To write a program to raise exception while user withdraws from account which have
low balance.
Source Code:
import [Link];
class InsufficientFundsException extends Exception
{
public InsufficientFundsException(String s)
{
super(s);
}
}
class CheckingAccount
{
double min=1000,balance=1000;
void deposit(double amt)
{
balance=amt+balance;
}
void withdraw(double amt)
{
try
{
if((balance-amt)<min)
{
throw new InsufficientFundsException("Insufficent balance");
}
else
{
[Link]("Amount Withdrawn");
balance=balance-amt;
}
}
catch(InsufficientFundsException e)
{
[Link](e);
}
}
}
public class Balance {
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
CheckingAccount d=new CheckingAccount();
int choice;
double amt;
19I433 [Link] Page 2
[Link]. : 07
Date : 21/10/2020
do
{
[Link]("[Link] [Link] [Link]");
choice=[Link]();
switch(choice)
{
case 1:
[Link]("Enter amount to withdraw");
amt=[Link]();
[Link](amt);
break;
case 2:
[Link]("Enter amount to deposit");
amt=[Link]();
[Link](amt);
break;
}
}while(choice!=3);
}
}
Output:
Result:
Thus to write a program to implement exception for insufficient funds was successfully
executed.
PROGRAM 3:
Aim:
To write a program to implement Number Format Exception.
Source Code:
public class Temp {
public static void main(String args[])
{
float a,b;
try
{
19I433 [Link] Page 3
[Link]. : 07
Date : 21/10/2020
a=[Link](args[0]);
b=(a*9/5)+32;
[Link](b + "degree Farenheit");
}
catch(NumberFormatException e)
{
[Link](e);
}
}
}
Output:
INPUT ARUGUMENT- 35
INPUT ARUGUMENT- 32.5
INPUT ARUGUMENT- Nav
Result:
Thus to write a program to implement Number Format Exception was successfully
executed using Exception.
PROGRAM 4:
Aim:
To write a program to calculate volume and to check the type of argument given is
float.
Source Code:
class IntegerNotValidException extends Exception
{
public IntegerNotValidException(String s)
{
super(s);
}
}
class StringNotValidException extends Exception
{
public StringNotValidException(String s)
{
super(s);
}
}
19I433 [Link] Page 4
[Link]. : 07
Date : 21/10/2020
public class Volume
{
public static void main(String[] args)
{
try
{
int b = [Link](args[0]);
throw new IntegerNotValidException("Integer Not
Accepted");
}
catch(IntegerNotValidException e)
{
[Link]([Link]());
}
catch(NumberFormatException g)
{
try
{
float a = [Link](args[0]);
double c,d,e,f;
c = a*a*a;
d = (4/3)*a*a*a*3.14;
e = (a*a*a)/3;
f = 3.14*(a*a*a);
[Link]("Volume of cube:"+c);
[Link]("Volume of Pyramid:"+e);
[Link]("Volume of sphere:"+d);
[Link]("Volume of
cyclinder:"+f);
}
catch(NumberFormatException f)
{
try
{
throw new
StringNotValidException("String not accepted");
}
catch(StringNotValidException d)
{
[Link]([Link]());
}
}
}
}
}
Output:
INPUT ARUGUMENT- aa
INPUT ARUGUMENT- 5
19I433 [Link] Page 5
[Link]. : 07
Date : 21/10/2020
INPUT ARUGUMENT- 3.2
Result:
Thus to write a program to calculate volume and to check the type of argument given is
float using Exception was successfully executed.
PROGRAM 5 (6question):
Aim:
To write a program to implement user defined exception to check the number is
negative and calculate its square root.
Source Code:
import [Link];
/**
*
* @author Viji
*/
class NegativeException extends Exception
{
public NegativeException(String s)
{
super(s);
}
}
public class Squareroot {
public static void main(String[] args)
{
double no;
Scanner n=new Scanner([Link]);
try
{
[Link]("Enter the number");
no=[Link]();
if(no<0)
throw new NegativeException("No square root of a negative
number");
else
[Link]("Square root of "+no+" :
"+[Link](no));
}
19I433 [Link] Page 6
[Link]. : 07
Date : 21/10/2020
catch(NegativeException e)
{
[Link]([Link]());
}
}
}
Output:
Result:
To write a program to implement user defined exception for square root of negative
numbers was successfully executed.
19I433 [Link] Page 7