2012年3月26日星期一

Coach Outlet

.introduction abnormality is determined by language provides a run time error handling in a way .Mention error handling ,even if not mentioned abnormalities ,you probably already have a wealth of experience ,but in order to you can clearly see the abnormalities of the benefits ,we may wish to review the commonly used and are not commonly used in error handling .
1.1 commonly used error handling the return value .We use the return value of the function to mark the success or failure ,and even failure reason .This approach however the biggest problem is that if the caller does not take the initiative to check the return value can also be permitted by the compiler ,you can do nothing to him :) it in C + + also leads to another question ,not only different is overloaded function return value ,and has the same parameter table ,because if the caller does not check the return value ,then the compiler will not know which overloaded function call .
Of course, this problem has nothing to do with this paper ,we put aside .Just remember the return value may be overlooked in the absence .Global status flag .For example system call using errno .
The return value is different, the global state flag can make function interface (return value ,parameter table ) are fully utilized .Function before the withdrawal should set the values of global variables ( including the reasons for success or failure ) ,and the return value ,it appears to require the caller to call check this logo ,this constraint is also weak .
Global variables also led to another question ,is a multi-threaded unsafe :if multiple threads at the same time as a global variable assignment ,then the caller in check this logo will definitely be very confused .
If you want the security thread, we can refer to errno solution ,it is thread safe .1.2 commonly used treatment methods ( setjmp ) / longjmp ( ) .May think they are remote goto statement .
According to my experience ,it seems really not been used ,perhaps much destruction of structured programming style .In C + +, should be more not to use them ,because the fatal weakness is longjmp ( ) unwinding stack ( although the word back again ) ,but not the call stack object - is fatal .
For different compilers ,Dr Dre Headphones,may by a compiler switch to solve this problem ,but is not universal ,will cause the program to the hard graft .Now let have a look 1.3 anomaly anomaly can solve any problem .
Return value and errno encountered embarrassment ,the abnormal is basically does not exist ,if you do not capture ( CATCH ) program exception thrown ,the default behavior is to lead to abort ( ) is called, the program was terminated ( core dump ) .
So if your function throws, this function of the caller or caller caller ,is also in the current call stack, there must be a place to catch the exception .As for the setjmp / longjmp ( ) ( ) the stack objects are not the destructor problem on abnormal is not exist .
So whether it destroyed the structured ( for OO paradigms ,perhaps I should say is destroyed in the process ?) ? Obviously not, with abnormalities after you can be assured only write the correct logic ,but all error handling due to a place ,it is not better ?To sum up ,in C + + or abnormalities can fully replace other error handling ,but if the code is filled with try / THROW / catch is not a good thing ,to know the abnormal use skill ,patience please continue reading :2 ) abnormal syntax here we only discuss some related problems of grammar .
2.1 try try is catch appear together ,with a try statement ,there should be at least one catch ( ) statement .Try then block might throw an exception .2.2 catch catch with one parameter ,parameter type and parameter names are specified by a program name ,can be ignored ,if the catch subsequent block is not intended to refer to the exception object .
Parameter type is build-in type ,such as int ,long ,char and so on ,can also be an object ,an object pointer or reference .If you want to catch exceptions of any type ,you can use the &ldquo ;&rdquo ;catch parameters as .
. .Catch try block must catch all exceptions thrown ,the rest not capture can give a function processing .2.3 throw throw back with an instance of a type ,its relationship with catch is like a function call ,catch specifies the parameter ,throw gives arguments .
The compiler according to catch appear in the order specified by the catch parameter and determine the type of an exception should be made by which catch to deal with .Throw does not have to appear in the try and block ,Coach Outlet Shop,it can appear at any required place ,as long as the final catch can capture it .
Even in the catch then block ,still can continue to throw .At this time there are two, one is throw, a new type of exception, with the general throw .The two is to rethrow this anomaly ,in this case ,throw without parameter expression .
For example :try {... } catch ( int ) {throw MyException ( " ;Hello exception" ;) ;/ / throw a new exception} catch ( float ) {throw ;/ / rethrow the floating point exception } 2.
4 function declaration and a place with the throw keyword ,is a function declaration .For example :void foo throw ( int ( ) ) ;/ / int void bar can only throw exceptions ( throw ( ) ) ;/ / does not throw any exception void Baz ( ) ;/ / can throw exceptions of any type or not to throw an exception if the declaration of a function with the throw qualifier ,then the function body must also be the same : void foo ( ) throw ( int ) {.
.. } here is a question ,very subtle, even though you like it prepared the foo ( ) function ,it can only throw the int specified abnormal ,but in fact it may still be thrown to the other types of abnormal and not be found :void compiler ( foo ) throw ( int ) {throw float ;/ / error !Abnormal type error !By the compiler to point out that.
.. Baz ( ) ;/ / correct !Baz ( int ) may throw an abnormal and the compiler cannot find !} void Baz ( ) {throw float ;} This is a direct consequence of if Baz ( ) throws, and calling foo ( ) code and strictly abide by the foo ( ) statement to write ,then the program will abort ( ) .
This made me very angry ,think this mechanism exists ,but there is a solution, please refer to &ldquo ;use the technique problems in &rdquo .3 abnormal use skills of 3.1 abnormal is to work in order to be safe use exceptions ,let us have a look of exception handling is how to work .
3.1.1 unwinding stack we know ,every function call happens, will be the implementation of protection field register ,stack ,parameters for the called function create stack that several of the stack operation ,they both make the stack grows .
Each time the function return is restoring the site ,so that the stack is reduced .The function returns the process of restoring the site by a process called unwinding stack .Exception handling in the throw statement and the function returns the same result ,it also caused unwinding stack .
If the catch is not in throw directly above function, then the unwinding process continues ,until you find the right catch .If no suitable catch ,then the last STD: :unexpected ( ) function is called ,that found an unexpected exception ,this function will call STD: :terminate ( ) ,terminate ( abort ) called the ( ) ,the program terminates ( core dump ) .
In the &ldquo ;&rdquo ;referred to in the longjmp ( ) can also be a unwinding stack ,but this is an C function ,it is just like free ( ) would not call an object ,it is also not known in unwinding stack during the call stack object .
This is the main difference between it and the anomaly .3.1.2 RTTI in unwinding stack process, the program will have been trying to find a suitable &ldquo ;&rdquo ;catch to handle the exception .
Earlier we mentioned that the throw and catch relationship is very much like a function call and the function prototype relationship, a number of catch as a function to be overloaded for can accept different types .
According to this speculation ,as if to find the right catch to handle the exception and function overloading in the process of finding suitable function prototype is the same, no big deal .
But the actual situation is very difficult ,because the overloaded call at compile time can be determined, and the exception is thrown but not ,consider the following code :void foo ( ) throw ( int ) {throw int ;} void bar ( ) {try {foo ( ) ;} catch ( int catch ( ) {.
.. } float ) {... } } void Baz ( ) {try {foo ( ) ;} catch ( int ) {... } catch ( float ) {... } } foo ( two ) in a place called ,these two abnormalities are different catch capture ,so for the throw generated code, not clear that to which the catch captured ,that is to say ,not at compile time determine .
Still consider this example ,let us have a look at compile time can not determine the fate of throw ,so how to determine at run time .In the bar ( ) ,a column in the switch statement catch like case arrangement ,is actually a series of judgment, check whether the type of the abnormal catch meet the specified type, the dynamically ,at run time to determine the type of technology is RTTI ( Runtime Type Identification / Information ) .
Depth exploration C + + object model is mentioned, RTTI is the exception processing by-products .On the RTTI is also a topic ,here is not discussed in detail .3.2 whether the heir to the STD :: exception ?Yes .
And STD: :exception has some derived classes ,if you need to use them directly ,without the need to repeat the definition .Each of the 3.3 function back to write throw ( ) ?Although front has been the analysis it is flawed, but it is still a good habit ,can let the caller from the documents are very clear information ,instead of turning those possible with code synchronization document .
If you provide a library ,Dr Dre Beats,then in the library entrance function should be used in the catch ( ... ) to catch all exceptions ,in catch ( ... ) to capture abnormalities should be converted ( rethrow ) is throw list in a certain abnormal ,so that does not produce an unexpected exception .
3.4 guard model of exception handling in unwinding stack, will the destructor all stack on the object ,but does not automatically delete the object on the heap ,and even your code while writing a delete statement ,but throw skip ,lead to memory leaks ,and other resource leak .
For example :void foo ( ) { MyClass P = new MyClass ... * ( ) ;bar ( P ) ;... Delete P bar ( ) ;/ / if thrown exception ,don run to here !} void bar ( MyClass * P ) {throw MyException ( ) ;} in this case ,the C + + provides a STD :: auto_ptr this template to solve the problem .
This is often referred to as a smart pointer &ldquo ;&rdquo ;template principle is the original code ,the pointer using a stack template instance is protected ,when exception occurs when unwinding stack ,the template instance will be destructed ,and in its destructor, the pointer will be delete ,e.
g. :void foo ( ) {... STD: :auto_ptr< ;MyClass> ;p ;( New MyClass ( ) ) ;bar ( p.get ( ) ) ;/ / ... Delete p ;/ / this sentence is no longer needed } void bar ( MyClass * P ) {throw MyException ( ) ;} bar ( regardless of whether an exception is thrown, ) as long as P is destructed ,the memory will be released .
Not only for memory, for other resource management also can refer to this method to do .In ACE ,this way is known as Guard ,used to lock protection .3.5 constructor and destructor constructor has no return value ,many places are recommended by throwing an exception to notify the caller structural failure .
This is definitely a good way ,but is not very perfect .Mainly because in the constructor and destructor throws an exception will not lead to the call ,for example :class foo {public: foo ( ) { } / / this function will be called } ;class bar {public: bar ( ) {C _ = new char ;throw 1 ;} ~ ( bar ) _ {delete C ;} / / this function will not be called !Private: char * C _ _ ;foo f ;} ;void Baz ( ) {try {bar B ;} catch ( int ) { } } in this example ,the bar destructor will not be called ,but even so ,foo destructors can still be called .
The danger is in the constructor of the allocated space in C _ ,because no destructor is called into leak .The best solution is to use auto_ptr ,auto_ptr ,bar statements into :class bar {public: bar ( ) {c_.
reset ( New char ) ;throw 1 ;} bar ( ) { } / / don delete C _ !Private: auto_ptr< ;char> ;C ;foo f _ _ ;;} ;the destructor is not an exception is thrown, the Thinking in In C + + Volume 2 has stated .
If the destructor call might throw an exception should be in the destructor function ,internal catch it .3.6 when using the abnormal until now ,we have finished the abnormalities of most of the problems, to practical operation .
Problems in actual application of my biggest problem is when should use exceptions, whether should use abnormal round instead of &ldquo ;&rdquo ;mentioned in other error handling ?First of all ,can not completely replace the abnormal return value, because the return value of the meaning is not just a success or failure, sometimes is a selectable state ,for example :if ( customer-> status ( ;;) = = active ) {.
.. } else {... } in this case ,regardless of whether the return value is what ,all are procedures can accept normal results .Abnormal expression of &ldquo &rdquo can be used ;anomaly ;-- the error state .
It is obviously that the actual programming ,but in the process there are many more ready to accept either course time, encountered such a situation ,Dr Dre Headphones,the first thing to consider is the principle .
Second ,have a look in the absence of specific abnormalities will play its advantages ,but this advantage is not to use other techniques to achieve ( or simply reach ) .For example ,if you are for a telecommunications company to write a complex charging logic ,then of course you want in the whole computational cost in the process of concentrated effort to consider the business logic problems ,but not everywhere need to return value to determine whether the current release in the previous step application resources .
When using the abnormal can make your code is very clear, even if you have 100 applications for resource place ,as long as a local concentration of the release of their good .For example :bool bar1 ( ) ;bool bar2 ( ) ;bool bar3 ( ) ;bool foo ( ) { char P1 = new char .
.. * ;( bar1 ... If !( ) ) {delete P1 ;return false ;} ... Char * P2 = new char ;if ( ... !bar2 ( ) ) {delete P1 ;/ / to be released for delete P2 in front of all the resources ;return false ;} .
.. Char * P3 = new char ;( bar2 ... If !( ) ) {delete P1 ;/ / to be released for delete P2 in front of all the resources ;delete P3 ;return false ;} } this process as is clear :void bar1 ( throw ) ( int ) ;void bar2 ( ) throw ( int ) ;void bar3 ( ) throw ( int ) ;void foo ( ) throw ( int ) {char * P1 = NULL ;char * P2 = NULL ;char * P3 = NULL ;try { char * P1 = new char ;bar1 ( ) ;char * P2 = new char ;bar2 ( ) ;char * P3 = new char ;bar3 ( ) ;} catch ( int ) {delete P1 ;/ / delete P2 delete concentrated release resources ;P3 ;throw ;} } third ,Coach Outlet,in Thinking In C + + Volume 2 of a when should not be used ,what should be used when the table ,We can refer to .
Finally, say a and abnormal unrelated things, but also with the program errors ,is the claim that ( assert ) ,I in the development in the use of the abnormalities of the posterior ,soon found that some people will use assert processing error is defined as abnormal .
Here a little note for assert usage ,very simple principle : only for those who can be achieved through improved procedures for correcting errors ,can only be used with assert .The return value ,abnormal obviously not at one level ,this is the C basic knowledge .
Related articles:

没有评论:

发表评论