2016-04-03 68 views
-2

當我創建自己的異常時,我開發了小型的AOP獨立項目,它給出了與throws clause.please不兼容的錯誤異常,如果任何人都可以幫助欣賞的話。與throws子句不兼容的異常

這是異常類我寫

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); 
    } 
} 

這是方法

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"); 
    } 
} 

這裏是主要方法

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

錯誤我得到 enter image description here

主要方法

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"); 
    } 








} 
+2

什麼問題? –

+0

我已經提到了錯誤的截圖 –

+1

你有一個空指針繼續。 –

回答

0

因爲該方法Payment()不與throws InsufficientAmountException聲明,您try...catch塊是無用的,這就是爲什麼你的錯誤。刪除try...catch塊,或修改Payment()以拋出InsufficientAmountException

相關問題