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