Java Programming thought study note _6

Source: Internet
Author: User

First, when it comes to break and continue statements, the finally sentence will also be executed.

 Public classTest7 { Public Static voidMain (string[] args) {inti = 0;  while(true) {            Try{i++; if(i = = 3)                     Break; } finally {                if(i = = 3) System.out.println ("HI");//Output Hi            }        }    }}

Second, the exception in the inheritance of the time need to pay attention to the details:

1, the exception limit to the constructor does not work, the subclass constructor can throw any exception, regardless of the base class throws the exception (this is different from the method), but because the base class constructor must be called in such or that way, the subclass constructor exception description must contain the base class constructor exception description.

class Dad {    publicthrows  aexception {            }}classextends  Dad {    publicthrows aexception,bexception/*aexception must be included in the Declaration  *  /        {Super();    }    }

2, the subclass method can not throw any exception, even if the base class definition of the exception, because if the base class method throws an exception, this does not break the existing program, so there is no problem.

 interface   Ainterface { public  void  f () throws   aexception;}  interface   Binterface { void  F () throws   bexception;}  class  Impl implements   Ainterface,binterface {@ Override  public  void   F () {}}  

3. If a subclass object is transformed upward into a reference to a base class, the compiler will require that the method be called to catch exceptions that might be thrown by the base class. Similarly, in the example above, in the main method, the compiler will require the exception to be handled by Ainterface:

 Public classTest8 { Public Static voidMain (string[] args)throwsaexception{ainterface Imp=NewImpl ();    IMP.F (); }}InterfaceAinterface { Public voidF ()throwsaexception;}InterfaceBinterface { Public voidF ()throwsbexception;}classImplImplementsAinterface,binterface {@Override Public voidf () {}}

4. The exception description itself is not part of the method type, which consists of the name of the method and the type of the parameter. Additionally, an exception that appears in the exception description of the base class method does not necessarily appear in the subclass.

Application of Getcause Method: (using Getcause processing to wrap other anomalies with abnormal chain)

 Public classTest6 { Public Static voidMain (string[] args) {Try {            NewA (). f (); }Catch(RuntimeException e) {System.out.println (E.getcause ());//output three. Aexception        }    }}classA { Public voidf () {Try {            Throw Newaexception (); } Catch(aexception e) {Throw NewRuntimeException (e); }    }}

Four, the principle of exception handling: Pay attention to handle the processing, but must not be important exceptions through the catch statement to capture the hidden.

  

Java Programming thought study note _6

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.