Talk about Java: CHECKED Exception Difference to Runtime Exception

xiaoxiao2021-03-06  46

There is a very important feature in Java that exception, that is, the program is allowed to generate exceptions. When learning Java, we only know that Exception is written, but it is not necessarily to understand the difference between different kinds of Exception.

First, you should know that Java provides two examples of Exception, one is an Exception (Runtime Exception) generated when executing, and the other is controlled Exception (Checked Exception). All Checked Exception is inherited from java.lang.exception, while Runtime Exception inherits java.lang.RuntimeException or java.lang.Error (actually java.lang.RuntimeException is also java.lang.exception) . When we write a program, we are likely to feel troublesome to choose some form of Exception. In the end, I should choose Runtime Exception or Checked Exception? In fact, in operation, we can use the Class's Method how to generate an Exception and how to handle this generated Exception to understand the differences between them. First, let's establish a Exceptionpublic class CException extends Exception {public CException () {} public CException (String message) {super (message);}} Then we write a possible CException of Classpublic class testException {public void method1 () throws CException {throw new CException ( "Test Exception");} public void method2 (String msg) {if (msg == null) {throw new NullPointerException ( "Message is null");}} public void method3 () throws CException {method1 () // The following omitted // ...} In these three Method, we have seen Exception in the program code of Method1 and Method2, but Method3's program code (within bracers) did not produce Exception, but in the definition of Method3, this Method may generate CEXCEPTION.

Call Method1 () programs must contain Method1 () in TRY and Catch, such as: public class runtest {// .... public static void main (string argv []) {testExtness te = new testException () Try {te.method1 (); CATIX (CEXCEPTION CE) {// ....}} // ...} Although it is included in TRY and CATCH, it does not mean that this program code will receive CEXCEPTION. But its intention is to remind the caller, perform accidents that the Method may be generated, and the user must have a corresponding processing method for this accident. When the user calls Method2 (), do not need to be packaged using TRY and CATCH, because of the definition of Method2, there is no throws any exception, such as: public class runtest {// .... public static void Main (String Argv []) {TESTEXCEPTION TE = New Testexception (); // Does not generate Exceptionte.Method2 ("Hello"); // Generate Exceptionte.Method2 (NULL);} // ...} program When executed, it is not necessarily generating nullpointerexception. This exception is called Runtime Exception, which is called unchecked exception, generating Runtime Exception's Method (Method2 in this example) does not need to define it when declared Method. Which Exception is generated?

In TestException's Method3 (), we have seen another situation, that is, Method3 is called Method1 (), but did not put the Method1 between TRY and CATCH. In contrast, in the definition of Method3 (), it defines the CEXCEPTION. It is actually if Method3 has received CEXCeption, which will not process this CEXCEPTION, and throw it outward. Of course, because of the definition of Method3, there is a THROWS CEXCEPTION, the program code for calling Method3 also needs to have TRY CATCH. So from the operation mechanism of the program, Runtime Exception is different from the Checked Exception, however, from logically, Runtime Exception is different from the Checked Exception on the purpose of use. In general, Checked Exception means that this Exception must be processed, that is, programmers should already know that a Exception may be received (because of try catch), the program designer should be able to target these different Checked Exception. Do different processing. Runtime Exception usually implies the error in the program, which will cause the programmer unable to process, and the program cannot continue. Take a look at the example below: String Message [] = {"Message1", "Message2", "Message3"}; system.out.println (Message [3]); this program code is no problem when compile, but When executed, the exception of ArrayIndexOutOfBoundException is displayed. Under this condition, we cannot make meaningful actions for this runtime exception, which is like we call Method2 in TestException, which triggered its nullpointerexception. In this case, we must modify the program code to avoid this problem. Therefore, in fact, we should also have to grab all Checked Exception, and it is best to make a corresponding process when these Checked Exception occurs, so that the program can face different conditions.

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

New Post(0)