overridden method cant throw exception
while compiling the following code i have got error
@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 Un开发者_如何学PythonSupportedCustomerRequestException extends Exception {
public UnSupportedCustomerRequestException(String bong){
System.out.println(bong);
}
}
its interface is
/*
* 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
You can't declare a overridden method to throw a broader exception.
In your case Exception is always broader than no exception
You cannot declare a new throws
clause in an overriding method. The interface needs to know about it. Or you could use unchecked exceptions.
You don't include the error but I suspect that the error is because you change the signature of the interface method with your throw statments so according to the compiler you have not implemented the method from the interface and you are overriding a method that is not in the interface.
When you implement an interface the functions have to be declared exactly as they are in the interface.
/Viktor
精彩评论