Exceptions in java involve the issue of parent-child class, which can be summarized into one sentence: the exception thrown by the constructor of a subclass must contain exceptions of the parent class. The method of the subclass can choose to throw an exception of the parent class "scope less than or equal to" or not throw an exception.
1. Why does the constructor have to throw an exception containing the parent class?
There is a passage in "thingking in java":
Exception limitation: When overwriting a method, only those exceptions listed in the exception description of the base class method can be thrown.
Exception restrictions do not work for the constructor, you will find that the StormyInning's constructor can throw any exceptions without paying attention to the exceptions thrown by the base class constructor. However, because the constructor must be called in one way or another, the exception description of the subclass constructor must contain the exception description of the base class constructor
This passage is a bit confusing at first, but you will understand after reading it:
First of all, the exception description is only for the override method, and the constructor is obviously not within this range, so the subclass constructor can throw any exception without taking into account the exception thrown by the parent constructor. However, when new a subclass object, the parent class constructor will definitely be called, so the exception thrown by the corresponding parent class constructor called by the subclass constructor must be taken into account. At this time, because "the subclass constructor cannot catch the exception thrown by the parent class constructor (which will be mentioned later)," the subclass constructor must throw this exception.
class SomeException extends Exception{} class TheOtherException extends Exception{} class BaseC { public BaseC()throws SomeException{} public BaseC(int a)throws TheOtherException{} } class SubC extends BaseC { public SubC() throws SomeException //If the exception is not thrown, an error will be reported { super(); //Super(37) is to be thrown; //If you replace super() with this, theOtherException must be thrown } }2. Why can't the subclass constructor catch the exception thrown by the parent class constructor?
Because if a subclass wants to catch the exception thrown by the parent class, it must display the call super(); or super(xxx...); however, super() and this() have a feature, which is that they must be placed in the first line, which contradicts try{}catch{}, so it cannot be captured
3. When the parent class and interface inherited by the subclass have the same method name, the processing method must follow the exception limit.
class SomeException extends Exception{} class TheOtherException extends Exception{} interface InterF { public void function()throws TheOtherException; } class BaseC { public void function()throws SomeException{} } class SubC extends BaseC implements InterF { //At this time you can only choose not to throw exception public void function(){} //An error: Exception SomeException is not compatible with throws clause in InterF.function() // public void function()throws SomeException{} ; // Error:Exception TheOtherException is not compatible with throws clause in BaseC.function() // public void function()throws TheOtherException{} ; }4. Why do subclasses only throw those exceptions listed in the exception description of the base class method?
Because the subclass has the possibility of upward conversion to the parent class, if the subclass is allowed to throw exceptions at will, then when the subclass is converted to the parent class, the interface of the method (let's call it that's the case) will become the method type of the parent class. At this time, the problem is that the subclass will throw exceptions, but the parent class cannot handle the exception. Therefore, in order to ensure the replaceable type of the object, it is mandatory that "only those exceptions listed in the exception description of the base class method are thrown."
The "exceptions" mentioned here also include these exceptions' sub-exceptions!
5. I don’t know if this counts. Maybe I am relatively stupid. I thought about it for a long time before I understood it. Let’s write it down for now.
class SomeException extends Exception{} class BaseC { public void function()throws SomeException{}//If the exception thrown here is a runtime exception subclass, it can not be handled exception} class SubC extends BaseC {<BR>//The super.function() performed by these two functions() belong to normal function calls and does not fall within the scope of exception handling, but this function itself must comply with the specifications of exception handling! /* public void function()throws SomeException { super.function(); } */ public void function() { try { super.function() ; } catch(SomeException e) { e.printStackTrace(); } } }The above detailed explanation of the exceptions involving father and child classes based on Java is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.