A test about an abnormal!

zhaozj2021-02-16  68

Who can tell me such a code, why not report the error? Class base {

Public void amethod () throws classnotfoundexception {

}} Public class Derived extends Base {public void aMethod () throws RuntimeException {}} I This question is this: parent class of aMethod thrown Base Derived subclasses and override aMethod ClassNotFoundException method throws an exception RuntimeException If you are in accordance with the Java's Exception theory, it should be: Subclass Derived's AMETHOD method either do not leveled, or you must throw ClassNotFoundException anomalous or ClassNotFoundException exceptions. And RuntimeException is not a subclass of ClassNotNotFoundException. Any inheritance relationship. According to the Java's Exception theory, it should not pass. But now you can compile, what this is why? RuntimeException is automatic ThROWS, do not need DecLare (Decal, no change, no change), even THROWS RUNTIME EXCEPTION, the compiler will not be processed according to the usual rules. So the equivalent form of the above code is: Class Base {public void amethod () throws classnotfoundexception {}} public class derived extends base {public void amethod () {}} ****** answer ***** java The abnormality can be divided into three types: 1. Abnormal class 2 directly derived from the Exception class. RuntimeException 3. I / O Exceptions You said that the theory of I / O for 1, 3, but RuntimeException is not necessary to go to catch. (Www.itpub.net)

转载请注明原文地址:https://www.9cbs.com/read-16589.html

New Post(0)