1. Basic grammar and semantics of standard C abnormal processing
This time, I will outline the basic grammar and semantics of standard C abnormal processing. Incident, I will compare it with the technique mentioned in the previous two. (In this article, I will refer to the standard C abnormality to EH, refer to Microsoft's method as SEH.)
1.1 Basic syntax and semantics
EH introduces 3 new C language keywords:
l catch
l throw
l try
Exception is triggered by the following speech
throw [expression]
The function defines what exceptions will be thrown through the "Abnormal Specification":
Throw ([Type-ID-List])
Options Type-ID-List includes one or more types of names, separated by commas. These exceptions are captured by the exception handler in the TRY block.
TRY Compound-Statement Handler-Sequence
The processing function queue contains one or more processing functions, the form is as follows:
Catch (Exception-Declaration) Compound-Statement
The "exception declaration" of the handler indicates what kind of exception will be captured.
Like SEH, the statement followed by TRY and CATCH must be scratched in {}, and the entire TRY block makes a full statement.
example:
Void f () throw (int, some_class_type)
{
INT I;
// ... generate an 'int' Exception
Throw i;
// ...
}
int main ()
{
Try
{
f ();
}
Catch (int E)
{
// ... Handle 'int' Exception ...
}
Catch (Some_Class_Type E)
{
// ... Handle 'Some_Class_Type' Exception ...
}
// ... Possibly other Handlers ...
Return 0;
}
Abnormal specifications have decided that EH is unique, SEH and MFC have no similar things. An empty abnormal specifications stated that the function does not throw any exception:
Void f () throw ()
{
// ... function throws no exception ...
}
If the function does not have an abnormal specification, it can throw any type of exception:
Void f ()
{
// ... function can throw anything or nothing ...
}
When the function throws the usual, the keyword throw is usually taken behind a thrown object:
Throw i;
However, Throw can also have objects:
Catch (int E)
{
// ... Handle 'int' Exception ...
Throw;
}
Its effect is to throw the object (Int E) currently being captured again. Because the effect of empty Throw is to throw an existing exception object again, it must be in the CATCH statement block. The MFC also has a function of throwing an exception. SEH did not, it did not hand over the abnormality object to the processing function, so there is nothing to throw again.
Like the parameters in the symbol of the function, the abnormality claims can also be nameless:
Catch (char *)
{
// ... Handle 'char *' Exception ...
}
When this processing function captures a char * type anomaly object, it cannot operate this object because this object has no name. Abnormal declaration can also be such a special form:
Catch (...)
{
// ... Handle Any Type of Exception ...
}
Just like "..." in an uncertain parameter, "..." in an abnormal state can match any exception.
1.2 Type of standard anomaly object
Standard library functions may report errors. An error in the C standard library said in the previous. In the C standard library, some functions throw a specific exception, while others do not throw any exceptions.
Because there is not clearly defined in the C standard, the C library function can throw any objects or do not throw. However, the implementation of the C standard recommended runtime is reported by throwing an exception type or its derived type in
Namespace STD
{
Class logic_error; //: Public Exception
Class domain_error; //: public logic_error
Class invalid_argument; //: public logic_error
Class length_error; //: public logic_error
Class out_of_range; //: public logic_error
Class runtime_error; //: Public Exception
Class Range_Error; / /: Public runtime_ERROR
Class overflow_error; //: public runtime_ERROR
Class underflow_error; //: public runtime_ERROR
}
These (abnormal) classes are only binding on the C standard library. In your own code, you can throw (and capture) any type you like.
1.3 other standals in the standard
Standard Liber File
Namespace STD
{
//
// Types
//
Class Bad_Exception;
Class Exception;
TYPEDEF VOID (* Terminate_Handler) ();
TYPEDEF VOID (* unnexpected_handler) ();
//
// functions
//
Terminate_handler set_terminate (TERMINATE_HANDLER) throw ();
Unexpected_handler set_unexpected (undexpected_handler);
Void Terminate ();
Void unExpected ();
Bool uncaught_exception ();
}
Summary:
l Exception is the base class that throws out of all standard libraries.
l UNCAUGHT_EXCEPTION () The function returns TRUE when there is abnormality that is not captured, and other situations return FALSE. It is similar to SEH's function abnormaltermination ().
l Terminate () is an emergency treatment of EH. It is called when the abnormality processing system is inseparable, often because it is trying to re-entry (an exception is thrown during the previous exception processes).
l Unexpected () is called when the function throws an exception declared in the "Abnormal Specification". This expected exception may be replaced with a Bad_Excetion object during the decorment process. The RT Runtuity provides the default TERMINATE_HANDLER () and UNEXPECTED_HANDLER () functions. You can replace the default version of the library through the set_terminate () and set_unexpected () functions.
1.4 abnormal life
EH runs five phases of the abnormal life period:
l Programs or runtime encountered an error condition (stage 1) and throw an exception (stage 2).
l The operation of the program stops in an abnormal point and starts searching for exception handling functions. Searching along the call to search (very like SEH termination).
l Search ends to find an exception declaration matching with the static type of the abnormality object (stage 3). Thus, enter the corresponding exception handler.
l After the exception handler is completed, jump to the closest statement below the TRY block where this exception handler is located starts (phase 5). This behavior means that abnormality in the C standard is always terminated.
These steps are demonstrated in this simple example:
#include
Static void f (int N)
{
IF (n! = 0) // stage 1
Throw 123; // stage 2
}
EXTERN INT Main ()
{
Try
{
f (1);
Printf ("Resuming, Should Never APPEAR / N);
}
Catch (int) // stage 3
{
// stage 4
Printf ("CAUGHT 'INT' EXCEPTION / N");
}
Catch (char *) // stage 3
{
// stage 4
Printf ("caught 'char *' exception / n");
}
Catch (...) // stage 3
{
// stage 4
Printf ("Caught Typeless Exception / N);
}
// stage 5
Printf ("Terminating, After 'Try' Block / N");
Return 0;
}
/ *
When Run Yields
CAUGHT 'INT' EXCEPTION
Terminating, after 'TRY' Block
* /
1.5 Basic Principle
The abnormality system of the C standard library has the following challenges when the C language:
l The destructor is ignored. Since the C standard library exception system is designed for C language, they don't know the C destructor. In particular, Abort (), exit () and longjmp () do not call the destructor of the local object when the stack or program terminates.
l Tedin. Query the global object or function return value caused code confusion - you must conduct clear abnormal conditions in all possible places, even though abnormalities may actually never happen. Because this method is so cumbersome, the programmer may deliberately "forget" to test anomalies.
l No elasticity. LongJMP () "Throw" can only be a simple int type. Errno and Signal () / raise () only use a small value domain collection, the resolution is very low. Abort () and exit () are always terminating the program. Assert () is only working in the Debug version.
L is non-intrinsic. All C standard library exception systems need to support support, it is not supported by language kernels. Microsoft's unique abnormal treatment system is not limited:
l SEH exception handler is not directly captured an exception object, but by querying a global value similar to errno, what is abnormal occurs.
l SEH exception handle functions cannot be combined, and the only one of a given TRY block must identify and process all exception events in runtime.
l The MFC exception handler can only capture the CEXCEPTION and the pointer of the derived type.
l The program contains hundreds of non-related macro and declarations by including the header file that defines the MFC exception handler.
L MFC and SEH are specially subject to a Microsoft compatible development environment and Windows running platform.
Standard C abnormalities avoid these shortcomings:
l destructure security. When leaving the neutrality, the destructor of the local object is called in the correct order.
l is not striking. Abnormal capture is in a dark and automatic. Programmers do not need to design due to error detection.
l Precise. Because almost any object can be thrown and captured, the programmer can control an exception content and meaning.
l Scalable. Each function can have multiple TRY blocks. Each TRY block can have a single or set of processing functions. Each processing function can capture a single type, a set of types, or all types of exceptions.
l Predictive. Functions can specify that they will throw an exception type, and the exception handler can specify what type of exceptions they capture. If the program violates its declaration, the standard library will run in a predictable, user-defined manner.
L is inherent. EH is part of the C language. You can define, throw and catch exceptions without need to contain any libraries.
l Standard. EH is available in all standard C implementations.
Based on the more complete ideas, the C Standards Committee considers two EH design, in D & E's 16 chapters. (For a more completion rate, incruding alternative eh designs considered by the C Standard's Committee, Check Out Chapter 16 of the D & E.)
1.6 small knot
Next time, I will explore the language core characteristics of EH and the standard library support for EH. I will also show Microsoft Visual C to implement the inside of EH. I will start to sign the Visual C of the EH, only partially supported or completely unsupported features, and find ways to bypass these restrictions.
While I believe that the basic principles of designing EH are sound, I also think that EH has no intention to include some serious consequences. It is difficult to understand the shortcomings of the C standard settlers, I understand how difficult the design and implementation of effective abnormalities are. When we encounter these unintentional consequences, I will show them the subtle impact on you, and recommend some techniques to mitigate their impact.