Orthomet of Java program design foundation

zhaozj2021-02-16  65

Early programming languages ​​(such as C language) have no abnormal processing, usually encountered an error to return a special value or set a flag, and determine whether there is an error in an error. As the system scale is expanding, this error handling has become an obstacle to creating large-scale maintenance procedures. Thus in some languages, exception handling mechanisms, such as the exception processing statement "on Error Goto" in Basic, and Java establishes a new exception handling mechanism based on C .

Java processes abnormally by object-oriented methods, and classifies a variety of different exceptions and provides a good interface. This mechanism provides a strong control method for complex programs. At the same time, these exception code are separated from "General" code, enhance the readability of the program, and more flexible when writing programs.

An exception handling has an advantage that when you cannot determine and deal with an exception, you can do not process, and submit the problem. On the other hand, the abnormal processing mechanism makes the error handling code more organizational and easier to maintain. Below, we look at how to deal with exceptions in Java. The anomaly class in Java is in Java, each anomaly is an object, which is an instance of the Throwable class or other subclass. When an abnormality occurs, an exception object is thrown, and the object contains abnormal information, and the method of calling this object can be captured and processed. The Throwable class has two standard subclasses: java.lang.Error and java.lang.exception ie errors and exceptions. Error error class  generally refers to a virtual machine related issue, such as system crash, virtual machine error, dynamic link fail, etc., this class error cannot be recovered or impossible to capture, will cause application interrupts. Abnormal Exception class refers to some abnormalities that can be captured and possible to recover. If the array subscript, the border, the border, the number, the number, the number is zero, and the ARITHMETICEXCEPTION  ARITHMETICEXCEPTION , the input / output exception  Exception  Tip: Java compiler requires a Java program to capture or declare all non-operating exceptions, such as FilenotFoundException, IOException, etc. Because, for such anomalies, if the program does not process, it may bring unexpected results. But there is an abnormality when running, as such exceptions are very common, all processing may affect the readability and running efficiency of the program. Java exception handling form Java's exception handling is achieved by 5 keywords: try, catch, throw, throws, and finally. Under normal circumstances, use TRY to perform a program, if an exception occurs, the system will throw an exception, then you can capture (catch) it through its type, or finally by default processor Treatment. Below is the basic form of the Java exception handler:

TRY  // Perform the program block catch EXCEPTIONTYPE1E    tExceptionType2e  // TreatmentTyPe2e  // Treatment THROW E  // throw this "exception" finally

The TRY block and the catch statement encountered a try statement, "exception" framework is placed on the stack until the statements in all TRY block are completed. If the TRY statement at the next level does not process some "exception", the stack will be expanded until the TRY sentence with this "exception" is encountered. Before the Try program, a Catch clause should be included to specify the type of "exception" you want to capture. Tip: The first step in capturing anomalies is to use TRY ...  Select the range you want to capture. When executed, the code in parentheses will generate an exception object and thrown. Then you can use the Catch block to handle an exception. The Throw statement and the throws statement throw statement is used to throw an "exception". First, you must get an instance handle generated by a Throwable class or other subclass, pass the parameters to the Catch clause, or use a new statement to create an instance. Below is the usual form of the throw statement: throw throwableInstance

Tip: After executing the throw statement, the runtime will stop immediately. The next statement of throw will also be suspended. Here the New operator is used to create an instance of a Throwable class, which will explain in detail in the object-oriented programming one after the following.

The throws statement is used to indicate a variety of "exceptions" that may be thrown by a member function. For most Exception subclasses, the Java compiler will force you to declare the type of "exception" thrown in a member function. If the type of "exception" is ERROR or RUNTIMEEXCEPTION, or their subclasses, this rule does not work. If you want to clearly throw a RuntimeException, you must use the THROWS statement to declare its type.

Tip: Of course, you may not understand the above instructions, but don't close, compile the following Java programs (I have made detailed comments for you), you will understand a lot!

Now let's explain the following routines:

class ThrowsDemo  // class name, the corresponding java source files should be saved to: ThrowsDemo.javastatic void procedure  throws IllegalAccessException // throws statement  // type of exception thrown IllegalAccessException System.out.println "inside procedure"   // Output text information to illustrate that the exception is thrown when the procedure method is thrown, "demo"                                                                                                                                     m             方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 方法 信息 信息 方法 方法 方法 方法 方法 方法 信息 方法 方法 信息 信息 信息 信息 方法 信息 信息 信息 方法 信息 信息 信息 信息 信息 信息 信息 信息// Handle the exception of the IllegaCcessException type, CATCH statement block system.out.println  "caught" E   

Program and execute the program, the process is as follows:

E / JavaCode / Exception> Java throwsdemoinside procedurecaught java.lang.illegalaccessException demo Demo

Tip: As shown above, we use a complete program to illustrate how to use Try ... Catch ... throw and other statements in the Java program. The exception generated in Try ...  is captured and then processed in the Catch   (here only output exception information). The Throws IllegaCcessException indicates that the program will throw an exception of the IllegaCcessException type. ILLEGALACCESSEXCEPTION E generates an exception object and then outputs exception information with System.out.Println. Finally statement Sometimes, in order to ensure that a piece of code is to be executed, you can use keyword Finally to mark such a code. A member function returns to the member function called it, or through an exception that is not captured, or through a clear returnite statement, the Finally clause will always execute before the member function returns. Below we describe the use of the use of the various exception handling statements described above with a comparison:

Class ExceptionDemo  // Exception Example, Saved as ExceptionDemo.javastatic void Proca                                                                                                    d System.out.println "proca's finally"    // Note: No matter what happens !! static void procb                                                                                                                             / Return, actually returning Finally  system.out.println  "" procb's finally "                                                                                                        Exception E  system.out.println  "catch" E Procb  

Tip: In the full program above, the code within the large brackets after Finally is executed any case. In addition, in Procb  , the return  statement is not immediately returned, and this should pay special attention. You can do your hand, try the above program (compile and perform similar to the above case)?

So far, the process control of Java has been completely finished. To really understand and master them only have a lot of exercises. A good way is to modify each of the above processes to achieve the features you want, and basically understand the Java process control. To write Java programs, familiar with process control and exception handling using Java are critical. So in these two periods, we use a large number of routines to explain how Java languages ​​use process control statements, and make a brief introduction to the frequently used exception handling statements in process control.

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

New Post(0)