2011-07-21 37 views
1

在編譯下面的代碼我有錯誤重寫方法斜面拋出異常

@Override 
public void routeCustomerRequest (int choice) throws UnSupportedCustomerRequestException{ 
    //throw new UnsupportedOperationException("Not supported yet."); 

    switch(choice) 
    { 
     case '1': 
      System.out.println("1. Add a new Customer"); 
      break; 
     default : 
      throw UnSupportedCustomerRequestException("hehehe"); 
    } 
} 

    // its constructor class is 

public class UnSupportedCustomerRequestException extends Exception { 
public UnSupportedCustomerRequestException(String bong){ 
    System.out.println(bong); 
} 

}

它的界面

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package bankingappication; 

/** 
* 
* @author Training 
*/ 
public interface IBankingServices { 
    public static String bankerResgistrationCode=null; 

    public void enquireCustomer(); 
    public int presentServiceMenu(); 
    public void routeCustomerRequest(int choice); 

    public boolean acceptDeposit(); 

    public boolean acceptCheque(); 
    public boolean processCheque(); 

    //customer name can be full or partial. 
    public boolean provideSummaryStatement(String customerName); 

    public boolean addCustomer(); 
    public boolean removeCustomer(); 
    public boolean updateCustomer(); 
} 

//please debug the error 

回答

6

你不能聲明一個重寫的方法拋出更廣例外。

在你的情況例外總是比也不例外

+0

我已編輯的程序更廣泛的,你認爲問題仍然存在 – hemanth

+0

是它應該。重新閱讀我的答案 –

1

你不能在一個壓倒一切的方法聲明一個新的throws條款。界面需要知道它。或者你可以使用未經檢查的異常。

1

你不包括錯誤,但我懷疑這個錯誤是因爲你用你的throw語句改變了接口方法的簽名,所以根據你沒有從接口實現該方法的編譯器,你重寫了一個方法不在界面中。

當你實現一個接口時,函數必須和接口一樣聲明。

/維克托