2016-03-26 23 views
1

在我的actionPerformed方法中,我調用「copy()」克隆對象,但編譯器給我這個錯誤:「java.awt.event.ActionListener;重寫方法不會拋出java.lang.CloneNotSupportedException 「, 我能做什麼?CloneNotSupportedException in actionPerformed

 public void actionPerformed(ActionEvent e){ 
    if (e.getSource() instanceof JButton) { 
     copy(); ... 

謝謝

回答

0

You can not add throwed checked exception to a method while overriding it.

[...] the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. [...]

你必須處理它。

@Override 
public void actionPerformed(ActionEvent e) { 
    //code 
    try { 
     copy(); 
    } catch (CloneNotSupportedException cnse) { 
     cnse.printStackTrace(); 
    } 
}