我讀到這段代碼的接口拋出一個異常,但實現它不拋出一個或趕上一個類,這是爲什麼?在java中合法還是安全?Java接口拋出異常,但接口實現不拋出異常?
import java.rmi.*;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{
public String sayHello() {
return "Server says, 'Hey'";
}
public MyRemoteImpl() throws RemoteException {}
public static void main (String[] args) {
try {
MyRemote service = new MyRemoteImpl();
Naming.rebind("RemoteHello", service);
} catch(Exception ex)
{
ex.printStackTrace();
}
}
}
您可能想查看http://www.coderanch.com/t/399874/java/java/Methods-throwing-Exception-Interface上的討論 – 2013-03-25 03:21:54
是的,這是合法的。你應該看到鏈接CHetter已經發布,而且他也應該可以發佈它作爲一個答案.. – Thihara 2013-03-25 03:23:31
感謝Thihara。已經這樣做了。 – 2013-03-25 03:24:53