java - At subclass Compilation Error: Unreachable catch block for CloneNotSupportedException -


in superclass, when override clone() method there no problem.

public class superclass implements serializable, cloneable {     public object clone() {         try {             return super.clone();         } catch (clonenotsupportedexception e) {             return null;         }     } } 

when override clone() method in subclass doing same (copy , paste clone() method super class) it's showing compilation error

public class subclass extends superclass implements serializable, cloneable {     public object clone() {         try {             return super.clone();         } catch (clonenotsupportedexception e) {             return null;         }     } } 

compilation error in subclasss @ line containing catch:

unreachable catch block clonenotsupportedexception. exception never thrown try statement body

again, if remove method clone() superclass subclass wont show error.

where gap of understanding?

if superclass's clone catches clonenotsupportedexception , doesn't throw it, subclass doesn't have catch it, since can never thrown try block of subclass's clone().

when remove clone() superclass, subclass calls object's clone() has throws clonenotsupportedexception clause. therefore have catch exception (or add throws clonenotsupportedexception method).


Comments

Popular posts from this blog

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -