0% found this document useful (0 votes)
5 views

Implementation of Java Program To Demonstrate Exception Handling

Uploaded by

harsita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Implementation of Java Program To Demonstrate Exception Handling

Uploaded by

harsita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

IMPLEMENTATION OF JAVA PROGRAM TO DEMONSTRATE

EXCEPTION HANDLING
PROGRAM CODE:
class NegativeSalaryException extends Exception {
private double m_salary;
private double other;
public NegativeSalaryException(String message, double m_salary, double other) {
super(message);
this.m_salary = m_salary;
this.other = other;
}
public double getMonthlySalary() {
return m_salary;
}
}

class NegativeWagesException extends Exception {


private double homeWages;
private double repairCharges;
public NegativeWagesException(String message, double homeWages, double
repairCharges) {
super(message);
this.homeWages = homeWages;
this.repairCharges = repairCharges;
}
public double getHomeWages() {
return homeWages;
}
}
class ExcessiveWagesException extends Exception {
private double totalWages;
public ExcessiveWagesException(String message, double totalWages) {
super(message);
this.totalWages = totalWages;
}
public double getTotalWages() {
return totalWages;
}
}

class Salary {
double m_salary;
double other;
Salary(double m, double o) throws NegativeSalaryException {
if (m < 0 || o < 0) {
throw new NegativeSalaryException("Salary or other earnings cannot be
negative.", m, o);
}
this.m_salary = m;
this.other = o;
}
double totalsalary() {
return m_salary + other;
}
}

class Wages {
double home_wages;
double repair_charges;
double medicinal_wages;
Wages(double wages, double charges, double medicine) throws
NegativeWagesException, ExcessiveWagesException {
if (wages < 0 || charges < 0 || medicine < 0) {
throw new NegativeWagesException("Wages or charges cannot be negative.",
wages, charges);
}
double total = wages + charges + medicine;
double threshold = 10000;
if (total > threshold) {
throw new ExcessiveWagesException("Total wages exceed reasonable limit.",
total);
}
this.home_wages = wages;
this.repair_charges = charges;
this.medicinal_wages = medicine;
}
double totalwages() {
return home_wages + repair_charges + medicinal_wages;
}
}

class Savings {
double tsalary;
double twages;
Savings(double sala, double wage) {
this.tsalary = sala;
this.twages = wage;
}
double savings_earned() {
return tsalary - twages;
}
}

public class Exp7 {


public static void main(String[] args) {
try {
double m_salary = -5000.0;
double other = 1200.0;
double home_wages = 7000.0;
double repair_charges = -1500.0;
double medicinal_wages = 6000.0;
Salary sal = new Salary(m_salary, other);
Wages wag = new Wages(home_wages, repair_charges, medicinal_wages);
double ts = sal.totalsalary();
double tw = wag.totalwages();
Savings sav = new Savings(ts, tw);
double se = sav.savings_earned();
System.out.println("TOTAL SALARY: " + ts);
System.out.println("TOTAL WAGES: " + tw);
System.out.println("TOTAL SAVINGS: " + se);
} catch (NegativeSalaryException e) {
System.out.println("Error: " + e.getMessage());
System.out.println("Monthly Salary: " + e.getMonthlySalary());
} catch (NegativeWagesException e) {
System.out.println("Error: " + e.getMessage());
System.out.println("Home Wages: " + e.getHomeWages());
} catch (ExcessiveWagesException e) {
System.out.println("Error: " + e.getMessage());
System.out.println("Total Wages: " + e.getTotalWages());
} finally {
System.out.println("Execution Completed");
}
}
}
OUTPUT:

You might also like