C, C ++ Exception Handling differences;

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.