-2

I am deveoloping small AOP standalone project there when i create my own exception it give this error exception incompatible with throws clause.please if any one can help appreciate very much.

This is the exception class i was written

package com.mtit.exceptions;

public class InsufficientAmountException extends Exception 
{
    /**
     * exception for handling invalid payment 
     */
    private static final long serialVersionUID = 1L;

    public InsufficientAmountException(String message)
    {
        super(message);
    }
}

This is the method

public void PaymentPatient(String PatientID,double discountRate,double ChargePerDay,double cashAmount) throws InsufficientAmountException 

{

    double totalAmount=CalculateTotalAmountPatient(PatientID,discountRate,ChargePerDay);

    if(cashAmount>=totalAmount)
    {
        double balance=cashAmount-totalAmount;
        System.out.println("Payment Succefully done your balance is "+"\t"+balance);

    }
    else
    {
        throw new InsufficientAmountException("Balance is insufficient");
    }
}

Here is the main method

try
    {
        //call the payment method i have written above
    }
    catch(InsufficientAmountException e)
    {
        System.out.println("");
    }

error i am getting enter image description here

main method

public class Main {

public static void main(String[] args) 
{


    try
    {
        Payment();
    }
    catch(InsufficientAmountException e)
    {
        System.out.println("");
    }



}
public static void Payment()
{
    PatientService patientService=new PatientServiceImpl();
    Scanner scanner=new Scanner(System.in);

    System.out.println("Enter Patient ID");
    String id=scanner.nextLine();

    System.out.println("Enter Discount rate");
    double discount=Double.parseDouble(scanner.nextLine());

    System.out.println("Enter No of Days");
    int noOfDays=Integer.parseInt(scanner.nextLine());

    double totalAmount=patientService.CalculateTotalAmountPatient(id, discount, noOfDays);

    System.out.println("Your total charge is"+"\t"+totalAmount);

    System.out.println("Would you like to do payment enter yes or no");
    String result=scanner.nextLine();

    if(result=="yes")
    {

        System.out.println("Enter charge per day");
        double ChargePerDay=Double.parseDouble(scanner.nextLine());

        System.out.println("Enter cash amount");
        double cashAmount=Double.parseDouble(scanner.nextLine());

        patientService.PaymentPatient(id, discount, ChargePerDay, cashAmount);

    }
    else

    {
        System.out.println("Please do the payment with in 3 days to get rid from tax");
    }








}
Siluni Upeksha
  • 419
  • 2
  • 6
  • 17

1 Answers1

0

Because the method Payment() isn't declared with throws InsufficientAmountException, your try...catch block is useless, that's why you get the error. Either remove the try...catch block, or modify Payment() to throw InsufficientAmountException.

Magnus
  • 17,157
  • 19
  • 104
  • 189