Java-related exceptions for parent-child classes _java

Source: Internet
Author: User
Tags exception handling first row throw exception

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

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

In "thingking in Java" There is a passage:

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

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

At first, this passage is a bit more round, but look at it again will understand:

First, the exception description is for the overriding method only, and the constructor is obviously not in the scope, so the subclass constructor can throw any exception without regard to the exception thrown by the parent class constructor. But when you're new to a subclass object, the parent class constructor is bound to be invoked, so the exception thrown by the corresponding parent class constructor called by the subclass constructor must be taken into account, and because the subclass constructor cannot capture the exception thrown by the parent class constructor (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 no exception is thrown the error 
  { 
    super ();//due to the default constructor of the base class being invoked, So to throw 
   the Someexception//super (37);//If you replace super () here, you must throw the Theotherexception 
  } 
   
}

2. Why does the subclass constructor not capture the exception thrown by the parent class constructor?

Because subclasses must call super () if they want to catch the exception thrown by the parent class; or super (XXX ...); However, super () and this () all have one feature, that is, they must be placed in the first row, which contradicts the try{}catch{} and cannot be captured

3. When a subclass inherits a parent class that has the same method name as an interface, then the processing 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 can only choose not to throw exception public 
  void function () {} 
   
  //error: Exception Someexception is not compatible With throws clause into interf.function () 
  //public void function () throws someexception{}; 
  Error: Exception Theotherexception is isn't compatible with throws clause into basec.function () 
  //public void function () th Rows theotherexception{}; 
}

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

Because subclasses exist the possibility of being converted up into a parent class, if a 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, and the problem is that the subclass throws an exception and the parent class cannot handle the exception. Therefore, to guarantee the interchangeability of an object, force the requirement "only those exceptions listed in the description of the exception of the base class method can be thrown."

The "exceptions" mentioned here also include these abnormal child exceptions!

5. This does not know calculate, perhaps I am more obtuse, I was looking at the time to think for a while to understand, let's 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, you can do no exception handling 
} 
 
class SUBC extends Basec 
{<br>//These two function () The Super.function () is a normal function call, not the scope of exception handling, but this function itself to conform to the exception handling specifications! 
/* Public 
  void function () throws Someexception 
  { 
    super.function (); 
  } 
*/Public
  void function () 
  { 
    try
    { 
      super.function (); 
    } 
    catch (someexception e) 
    { 
      e.printstacktrace (); 
    } 
  } 
}

The above is based on Java related to the exception of the parent-child class is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

Related Article

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.