Professional Documents
Culture Documents
Exceptions: Program 1: Aim: Source Code
Exceptions: Program 1: Aim: Source Code
: 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 java.util.*;
try {
if(age < 18)
throw new AgeException("Invalid age");
else
System.out.println("Valid age");
}
catch (AgeException a) {
System.out.println(a);
}
}
}
Output:
PROGRAM 2:
Aim:
To write a program to raise exception while user withdraws from account which have
low balance.
Source Code:
import java.util.Scanner;
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
{
System.out.println("Amount Withdrawn");
balance=balance-amt;
}
}
catch(InsufficientFundsException e)
{
System.out.println(e);
}
}
}
public class Balance {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
CheckingAccount d=new CheckingAccount();
int choice;
double amt;
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
{
Output:
INPUT ARUGUMENT- 35
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);
}
}
try
{
int b = Integer.parseInt(args[0]);
throw new IntegerNotValidException("Integer Not
Accepted");
}
catch(IntegerNotValidException e)
{
System.out.println(e.getMessage());
}
catch(NumberFormatException g)
{
try
{
float a = Float.parseFloat(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);
System.out.println("Volume of cube:"+c);
System.out.println("Volume of Pyramid:"+e);
System.out.println("Volume of sphere:"+d);
System.out.println("Volume of
cyclinder:"+f);
}
catch(NumberFormatException f)
{
try
{
throw new
StringNotValidException("String not accepted");
}
catch(StringNotValidException d)
{
System.out.println(d.getMessage());
}
}
}
}
}
Output:
INPUT ARUGUMENT- aa
INPUT ARUGUMENT- 5
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 java.util.Scanner;
/**
*
* @author Viji
*/
class NegativeException extends Exception
{
public NegativeException(String s)
{
super(s);
}
}
Output:
Result:
To write a program to implement user defined exception for square root of negative
numbers was successfully executed.