C ++ Exception Handling, exception declaration, and exception capturing

Source: Internet
Author: User

 
C ++


Rules for capturing exceptions:

 

1.

Non-MFC
C ++
The exception should be captured by reference. You do not need to delete C ++ to capture exceptions using references.
Exception object. Because the exception object captured by reference will be passed in the stack. In addition, the polymorphism is maintained so that the exception you capture is exactly the exception object you throw. To catch exceptions using pointers, You need to delete the exception object. If the value is captured, the object's slicing
Problem. For more details, see Objective C ++.
13th
.

2. MFC
Exceptions should be captured by pointers. Because they are usually allocated from the heap. After processing the exception, you need to call Delete
Member functions. As follows:

Try

{

......

}

Catch (cfileexception
* E)

{

//
Exception Handling

E-> Delete ();

}

Therefore, you cannot omit (catch (...))
To capture MFC
Exception,
This usually causes memory leakage. Delete is required.
Member functions to delete exceptions, rather than using Delete
Because some MFC
An exception is created as a static object.

Once an exception is captured, you can perform a combination of the following typical actions:


Nothing


Modify the problem and re-execute the code


Modify the problem but do not re-execute the code


If necessary, the error message is displayed to the user.


If the problem is not a program error, output a trace diagnosis message.


If the problem is a program error, an asserted is output.


Record this issue in the log file


If the exception is unrecoverable, terminate the process.


Reclaim allocated resources


Throw this exception again or a new exception.

 

Use the default capture processor (catch (... ))

The default capture processor can capture any type of exceptions. Including system generation and program exceptions. Its behavior and switch
Statement default
Similar. This processing method is very powerful and easy to use. However, improper use is harmful because you are not sure what is going on. What you know is that some unknown exceptions have occurred. Therefore, you cannot know whether the current function is suitable for handling exceptions, or whether it is suitable for processing higher-level functions. Note that improper exception handling may damage your program or cause other types of damage. Therefore, try not to use the default exception capture processor.

However, in the following two cases, it is appropriate to use the default capture processor:



The processor will throw the same exception or another exception again. The default capture processor is often used.

To sort resources or throw an exception again.



The exception processor ends the process under any circumstances.

Do not throw an exception in the destructor, which usually causes Process Termination. I have debugged this situation more than once. Poor Exception Code makes the program worse.

 

Exception statement specification:

C ++
Exception statement specifications provided:

 

//
A normal function declaration can throw any exception

Void normalfunction ();

 

//
Do not throw an exception

Void nothrowfunction throw ();

 

//
Only throw the specified exception

Class exceptionclass;

Void throwfunction () Throw (exceptionclass );
//
Including predictionclass
.

 

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.