Article title: Continuation and advanced process control. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Process control is usually very simple: including serialization, selection, iteration, and other processes. Many programmers who have been using these basic control structures have experienced some difficult time to determine which process control is necessary. This article will briefly introduce the content of continuation and show you how to use the latest method to consider the issue of process control.
Many programmers do not think too much about process control after their first access to programming, because most programming languages have only a few simple process control structures. However, the content of process control is actually richer than that provided by most mainstream programming languages. Many languages not known to the majority have advanced and useful process control structures.
Examples of advanced process control
The best way to get started with process control is to look at several examples of advanced process control structures in different languages. This knowledge can then be summarized into a framework suitable for advanced process control.
Large-scale exit
You may have heard of the first advanced process control technology: non-local exit ). There are several types of large-scale exit, each of which can be divided into two categories: structured and unstructured.Unstructured non-local exit)The computer science teacher warned you not to do this, for example, terriblegoto
Statement. In fact, an unstructured large-scale exit may be very useful if it is widely and correctly used. For example, process control can improve the readability of programs in complex programs. If complex process control cannot be nested naturally, forcing the nested structure will make the entire program more readable, rather than better. Usagegoto
For more information about the advantages and disadvantages of statements, refer to the links provided in the References section later in this article.
ForStructured large-scale exit (structured non-local exit)You may be familiar with one of the most popular types: exceptions. If you have been using C, Fortran, and Cobol for more than 20 years and have never used other languages, see the following section about exceptions.
Exception)It is a method to trigger an error in code or to localize the error. Generally, when an error occurs, we want to handle it. The rest of the work will not continue unless we explicitly give instructions for further operations. For example, we can see that Java™The simple database code written in the language:
Listing 1. simple database code example
//NOTE -- this code will not compile, but that's on purpose.import java.sql.*; ... public static int testdbcode(Connection conn) { //Prepare the statement PreparedStatement s = conn.prepareStatement( "select id from test_table where name = ?"); //Set the parameters s.setString(1, "fred"); //Run the query ResultSet rs = s.executeQuery() //Move to the first result row rs.next(); //Get the answer (first result parameter) int id = rs.getInt(1); return id; } ...
|
The problem with this code is that there is no error handling, and errors may occur everywhere when processing databases or other external entities. For example, if an error occurs during the preparation of the query, it is useless to set parameters, run the query, and search results. This query is meaningless after the first error is encountered. Therefore, some exceptions are provided in the Java language, which allows us to encapsulate a piece of code, so that the code will jump to the first error. To implement this function in Java, we can rewrite this code as follows:
List 2. simple database functions with exception handling
import java.sql.*; ... public static int testdbcode(Connection conn) { try { //Prepare the statement PreparedStatement s = conn.prepareStatement( "select id from test_table where name = ?"); //Set the parameters s.setString(1, "fred"); //Run the query ResultSet rs = s.executeQuery() //Move to the first result row rs.next(); //Get the answer (first result parameter) int id = rs.getInt(1); return id; } catch (Exception e) { //Put error handling code here return -1; } } ...
|
try
The code block in is always executed, unless an error occurs in a statement. IfTriggerAn error is skipped.try
Code block in, executecatch
Code block, where variablese
The error message is saved. In Java, all the errors triggered are complete classes, so any information can be put into exceptions. In fact, we can create multiplecatch
Code block. each code block is used to handle a specific exception class.
In the preceding code example, we handle system-generated exceptions. However, we can also create application-level exceptions and perform the same processing on them. Applications can be used at any timethrow
Keyword trigger exception. For example, we can say that if the ID value in the above code is 1, it can be considered as an application-level exception. We can use the following method:
Listing 3. example of a simple database that triggers an application-level exception
import java.sql.*; ... public static int testdbcode(Connection conn) { try { //Prepare the statement PreparedStatement s = conn.prepareStatement( "select id from test_table where name = ?"); //Set the parameters s.setString(1, "fred"); //Run the query ResultSet rs = s.executeQuery() //Move to the first result row rs.next(); //Get the answer (first result parameter) int id = rs.getInt(1); //Check for application exception if(id == 0) { //Signal the exception //Assume that InvalidUserIDException is defined elsewhere throw new InvalidUserIDException(); } return id; } catch (InvalidUserIDException e) { //Specialized error handling here return -1; } catch (Exception e) { //Handle all other errors here return -1; } } ...
|
In addition, there is no reason for the code to handle exceptions to be placed in the function itself.try
Andcatch
Statements can also be placed in any containing function. The exception handling mechanism expands the stack until it reaches a suitable exception handling program, and the program executes the exception handling code. This ability to execute structured large-scale exit can greatly simplify code writing and maintain the code, because in some cases, error handling is completely separated from the code that actually implements the function. Of course, this is all very simple. There are several main functions for exception handling, which are beyond the scope of this article, but some of the content in the references can help you in this regard.
[1] [2] [3] [4] [5] Next page