Java involves a parent-child class exception

Source: Internet
Author: User

Exceptions in Java involve the problem of parent-child classes, which can be summed up in a sentence: The exception that is thrown by the constructor of a subclass must contain the exception of the parent class, and the method of the subclass can optionally throw an exception of "scope less than or equal to" the parent class or not throw an exception.

1. Why does the constructor have to throw an exception that contains the parent class?

In "thingking in Java" There is a passage:

Exception limit: When overriding a method, only those exceptions listed in the exception description of the base class method can be thrown

The exception limit does not work for the constructor, and you will find that the Stormyinning constructor can throw any exception regardless of the exception thrown by the base class constructor. However, because the constructor must be called in such a way or another, the exception description of the subclass constructor must contain the exception description of the base class constructor

This paragraph at the beginning of a comparison around, but Hee to see again will understand:

First, the exception description is only for the overriding method , and the constructor is obviously not in this range, so the subclass constructor can throw any exception, regardless of the exception thrown by the parent class constructor. However, when a subclass object is new, the parent constructor must be called, so that the exception thrown by the corresponding parent constructor of the subclass constructor call must be taken into account, and because the subclass constructor cannot catch the exception thrown by the parent class constructor (which is mentioned later ). So 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 you do not throw an exception, you will get an error {super ();///because the default constructor of the base class is called, so throw someexception  //super (37);//If Super () is replaced here, Must be thrown theotherexception}}

2. Why can't the subclass constructor catch the exception thrown by the parent class constructor?

Because subclasses must call super () when they want to catch the exception thrown by the parent class; or super (XXX ...); However, super () and this () have a feature that they must be placed on the first line, which contradicts the try{}catch{}, so it is not possible to capture

3. When a subclass inherits the same method name as the parent class and interface, the exception restriction must be followed.

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{//you can only choose to not throw exception public at this time void function () {}//error: Exception someexception is not compatible with throws clause in interf.function ()//public void funct Ion () throws someexception{}; Error: Exception Theotherexception is not compatible with throws clause in basec.function ()//Public  void function () thr oWS theotherexception{};}

4. Why can subclasses only throw those exceptions listed in the exception description of the base class method?

Because the subclass has the possibility of turning upward into a parent class, if the subclass is allowed to throw an exception, then the interface of the method ( let's call it ) becomes the method type of the parent class, when the problem comes, the subclass throws an exception, and the parent cannot handle the exception. Therefore, to ensure that the object is replaceable, it is mandatory to "throw only those exceptions listed in the exception description of the base class method."

The "exceptions" mentioned here also include the sub-anomalies of these exceptions!

5. This point does not know count, perhaps I am more stupid, I was looking at the time to think for a while to understand, just write it down.

Class Someexception extends Exception{}class basec{public void function () throws someexception{}// If the exception thrown here is a run-time exception subclass, it can not be handled abnormally}class SUBC extends basec{
The Super.function () of the two function () is a normal function call, not a range of exception handling, but the function itself conforms to the specification of exception handling!/*public void function () throws Someexception{super.function ();} */public void function () {try{super.function ();} catch (Someexception e) {e.printstacktrace ();}}}

  

Thinking in Java note, if there is a wrong place, also look at ^_^

Java involves a parent-child class exception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.