Java exception Handling throw runtimeexception does not require simultaneous method declaration thrown throws exception waits for caller catch to catch child parent exception problem

Source: Internet
Author: User

 PackageCom.swift.exception1; Public classdemo_exception { Public Static voidMain (string[] args) {int[] arr=New int[] {2,5,3,5,4}; Try{Array (arr); }Catch(Exception e) {System.out.println ("Resolve this exception ~ ~");        E.printstacktrace (); }    }    Private Static voidArrayint[] arr)throwsexception{if(arr.length>=5) {            Throw NewIndexoutofboundsexception ("array subscript out of bounds exception throws ~~~~~~~~"); }        intK=arr[6];        System.out.println (k);  for(intx=0;x<arr.length;x++) {System.out.println (arr[x]); }    }}

RuntimeException can also give throws

Non-running Exception (compilation exception) The throw must require a throws exception to be caught or continue to be thrown because a run-time exception occurs and the program stops

Child Parent Class Exception problem

Subclass exception cannot be greater than parent class exception

Parent class no exception, subclass cannot have exception

Parent class has exception, subclass can be no exception

The reason is that because of inheritance, the method is caused by the problem of replication, the polymorphic parent reference is called by the subclass of the method of replication,

Class fu{

public void Fun () throws Exception () {

}

}

Class Zi extends fu{

public void Fun () throws Throwable () {//greater than parent exception, compile but

}

}

Class test{

public static void Main (String args[]) {

Fu f=new Zi ();//parent class reference Call

F.fun ();//Sub-class method,

}

}

That is, the subclass exception can not be exceeded, the parent class can be covered.

Java exception Handling throw runtimeexception does not require simultaneous method declaration thrown throws exception waits for caller catch to catch child parent exception problem

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.