You are on page 1of 7

Ex.No.

: 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.*;

class AgeException extends Exception {

public AgeException(String str) {


System.out.println(str);
}
}
public class Age{

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.print("Enter your age :: ");
int age = s.nextInt();

try {
if(age < 18)
throw new AgeException("Invalid age");
else
System.out.println("Valid age");
}
catch (AgeException a) {
System.out.println(a);
}
}
}

Output:

19I433 G.NAVEEN Page 1


Ex.No. : 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 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;

19I433 G.NAVEEN Page 2


Ex.No. : 07
Date : 21/10/2020
do
{
System.out.println("1.Withdraw 2.deposit 3.exit");
choice=s.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter amount to withdraw");
amt=s.nextDouble();
d.withdraw(amt);
break;
case 2:
System.out.println("Enter amount to deposit");
amt=s.nextDouble();
d.deposit(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 G.NAVEEN Page 3


Ex.No. : 07
Date : 21/10/2020
a=Float.parseFloat(args[0]);
b=(a*9/5)+32;
System.out.print(b + "degree Farenheit");
}
catch(NumberFormatException e)
{
System.out.println(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 G.NAVEEN Page 4


Ex.No. : 07
Date : 21/10/2020
public class Volume
{
public static void main(String[] args)
{

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

19I433 G.NAVEEN Page 5


Ex.No. : 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 java.util.Scanner;

/**
*
* @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(System.in);
try
{
System.out.println("Enter the number");
no=n.nextDouble();
if(no<0)
throw new NegativeException("No square root of a negative
number");
else
System.out.println("Square root of "+no+" :
"+Math.sqrt(no));
}

19I433 G.NAVEEN Page 6


Ex.No. : 07
Date : 21/10/2020
catch(NegativeException e)
{
System.out.println(e.getMessage());
}
}
}

Output:

Result:
To write a program to implement user defined exception for square root of negative
numbers was successfully executed.

19I433 G.NAVEEN Page 7

You might also like