C ++ Gotchas Terms 64: Throwing String Literals

zhaozj2021-02-08  498

Gotcha # 64: Throwing String Litrals

Gotcha Terms 64: Throwing String Literals

Many C programming students throw the character string (Character String Litrals) when displaying an abnormal mechanism:

Throw "stack underflow!";

They know that this practical method should be avoided, but they still do this because it is just "teaching example". Unfortunately, this example is implied "Imitation this example" (translation: After all, readers will certainly tend to follow the examples in the present invention to learn), and these authors usually ignore the readers : "If you really do this, you will cause delimitation and bad luck."

Never throw string Litrals as an Exception Objects. In principle, this is: These Exception Objects should eventually be captured, and should be captured according to their type (TYPE), not based on its value (Value) to capture:

Try {

//.

}

Catch (const char * msg) {

String M (MSG);

IF (m == "stack underflow") //.

ELSE IF (m == "connection timeout") //.

ELSE IF (m == "security viological") //.

Else throw;

}

Throwing and capturing the actual impact generated by String Litrals is that there is almost no information about exceptions via the exception Object. This unstestive approach requires that "Catch Clause intercepts every similar abnormality and judges whether to capture the condition" by viewing its value (Value). Worse, the comparison of the value is also very strict; once the "error message" has changed in case or format, this comparison is invalid. If this happens in the above example, we will never find the situation of "Stack underflow".

The same situation also exists in other predefined types and standardized abnormalities. Throwing Integers, Floating Point Numbers, Strings, or (in a bad day) SETS consisting of Float Vector, will trigger a similar problem. Simply put, the issue of "throwing a predefined 别 别 异 异 对" is: When we capture a such anomaly, we can't know what it means, and it is not possible to determine how to handle exceptions. Those who throw this abnormally seem to be fooled: "Very very very bad thing! What do you guess?" And we have no choice, I have to play the guessing game and it is likely to play.

The so-called Exception Type is an abnormally data type (Abstract Data Type). Designing these abnormalities do not much from designing any other abstract data type: Identify and name a concept, which is a collection of abstract operations and implements it. In the process of implementation, problems such as initialization, copy, and type conversion are considered. Very simple. With String Litral to represent an exception, it is not much more meaningful than using Complex Number. In theory, this may be effective, but from actually telling this will become long and boring, bugs. When we throw out the exception of Stack Underflow () ", what kind of abstract concept we try to express? Oh. Can it be it?

Class Stackunderflow {};

Typically, the type of Exception Object can communicate all information about this exception; and for Exception Types, "Dispense" is not unusually unusual for information purposes. However, "the ability to provide descriptive text" is often believed. Other related information that is not very often need can also be recorded in Exception Object:

Class stackunderflow {

PUBLIC:

Stackunderflow (const char * msg = "stack underflow");

Virtual ~ stackunderflow ();

Virtual const char * what () const;

//.

}

If a function that returns a descriptive text is provided, it should be a Virtual Member function called WHAT, which has the above expression. This is to take into account this exception and standard abnormal orthogonality, because the standard abnormal types provide this function. In fact, let customized abnormalities don't derive self-standard anomalous, usually a good idea:

Class Stackunderflow: Public std :: runtime_error {

PUBLIC:

Explicit Stackunderflow (const char * msg = "stack underflow")

: std :: runtime_error (msg) {}

}

This allows us to capture it as Stackunderflow, more general runtime_error, or more general standard exceptions (Runtime_ERROR's Public Base Class) to capture. Provide a more general but non-standard Exception Type, usually also a good idea. In general, this type is used as Base Class, all Exception Types that may thrown by a specific module or library is derived from it:

Class containerfault {

PUBLIC:

Virtual ~ containerfault ();

Virtual const char * what () const = 0;

//.

}

Class Stackunderflow

: public st: runtime_error, public containerfault {

PUBLIC:

Explicit Stackunderflow (const char * msg = "stack underflow")

: std :: runtime_error (msg) {}

Const char * what () const

{RTURN std :: runtime_error :: what ();

}

The last thing to say is that it is also necessary for Exception Types to provide ordinary Copy and Destruction semantics. In particular, throwing an exception suggests that "Copy Construct This Exception Type object must be legitimate", because this is the event that the runtime exception mechanism is doing when the exception is thrown out (see Gotcha Terms 65), and This copy must be destroyed when the exception is processed. It usually allows the compiler to automatically write these operations for us (see Gotcha Terms 49):

Class Stackunderflow

: public st: runtime_error, public containerfault {

PUBLIC:

Explicit Stackunderflow (const char * msg = "stack underflow")

: std :: runtime_error (msg) {}

// stackunderflow (const stackunderflow);

// stackunderflow & operator = (const stackunderflow);

Const char * what () const

{RTURN std :: runtime_error :: what ();

}

Now, STACK TYPE can choose from it, let Stack Underflow are captured as different levels of exception type, which can be Stackunderflow (users know that they are using Stack Type, I hope to pay special attention to it), more general ContainerFault (user know I am using Container Library, I want to capture any container caused by errors), Runtime_ERROR (users don't know which Container Library you use, want to handle any standard running period error), or the most general EXCEPTION (user preparation processing any standard exception ).

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

New Post(0)