Implementation of Java Program To Demonstrate Exception Handling
Implementation of Java Program To Demonstrate Exception Handling
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 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;
}
}