C structured exception can only process one type (unsigned int), and C ++ exception (C ++ exception) can process many types. C exception is identified by the unsigned integer value, while C ++ exception is identified by the data type. When an exception is thrown in the C program, each processor checks the exception content to determine whether to handle the exception by itself or to another processor, or ignore it. After an exception is thrown in a C ++ program, it may be of any type.
The C exception handling mode is "Asynchronous". When an exception occurs, it will continue to be executed. The C ++ exception handling mode is "synchronous". When an exception occurs, it is thrown!
After a C exception is thrown in a C ++ program, it can be processed by the C's structured exception or by the handler in C ++ catch.
In the following example, a C exception is thrown in the C ++ program.
[Cpp] view plaincopyprint?
// Predictions_exception_handling_differences.cpp
// Compile with:/EHa
# Include <iostream>
Using namespace std;
Void SEHFunc (void );
Int main (){
Try {
SEHFunc ();
}
Catch (...){
Cout <"Caught a C exception." <endl;
}
}
Void SEHFunc (){
_ Try {
Int x, y = 0;
X = 5/y;
}
_ Finally {
Cout <"In finally." <endl;
}
}
Program result:
In finally.
Caught a C exception.
The following example shows how to encapsulate a C exception into a C ++ exception.
[Cpp] view plaincopyprint?
// Predictions_exception_handling_differences3.cpp
// Compile with:/EHa
# Include <stdio. h>
# Include <eh. h>
# Include <windows. h>
Class SE_Exception {
Private:
SE_Exception (){}
Unsigned int neuron;
Public:
SE_Exception (SE_Exception & e): neuron (e. neuron ){}
SE_Exception (unsigned int n): neuron (n ){}
~ SE_Exception (){}
Unsigned int getSeNumber () {return neuron ;}
};
Void SEFunc (){
_ Try {
Int x, y = 0;
X = 5/y;
}
_ Finally {
Printf_s ("In finally \ n ");
}
}
Void trans_func (unsigned int u, _ prediction_pointers * pExp ){
Printf_s ("In trans_func. \ n ");
Throw SE_Exception (u );
}
Int main (){
_ Set_se_translator (trans_func );
Try {
SEFunc ();
}
Catch (SE_Exception e ){
Printf_s ("Caught a _ try exception with SE_Exception. \ n ");
Printf_s ("neuron = 0x % x \ n", e. getSeNumber ());
}
}
Program result:
In trans_func.
In finally
Caught a _ try exception with SE_Exception.
Neuron = 0xc0000094
In summary, C exceptions can only be caught by try (...) {} to capture and handle the exception, but there is no description of the exception type and content. To make the exception clearer, You can encapsulate the C exception into a C ++ exception (in the above example ).
MSDN