This exception is not an exception.

Source: Internet
Author: User

Reprinted please indicate the source

Author: Pony

 

Recently, more and more people are using try/catch in C ++. I found many people mistakenly understand the try/catch usage. I have actually encountered this misunderstanding when I first learned C ++, I wrote an article to discuss this issue. I hope you can avoid such mistakes.

 

When I first learned how to handle C ++ exceptions, I think try/catch is omnipotent. The so-called "omnipotent" is the code that I think adds exception handling, I can handle errors such as Division 0 and invalid memory operations. for example:

try{ int *p = NULL; *p = 10;}catch (...){ cout <<"catch the exception"<<endl";}

At first, I think my program will not crash. Although there is an illegal memory write operation (assigning 10 values to the memory address 0), I will catch it,
The program result should be output:
Catch the exception

Later, I knew that I was wrong. In the past, try/catch only caught language-level exceptions, that is, exceptions thrown by throw in the program. For example:

try{ int *p = NULL; throw "this is a exception"}catch (...){ cout <<"catch the exception"<<endl";}

The result of the program running is output:
Catch the exception

I want to see that many people have understood this. But the article is not over yet. I want to say a few more words.

 

What is the correct running result of the first code above? In most cases, an error dialog box is displayed, and the process ends. this indicates that your program has crashed. first, make it clear that this exception is called an illegal memory read/write operation. Since the operation system will pop up a dialog box, it indicates that it has handled this exception (the dialog box is displayed, and then the process is forcibly ended, the handling method is a bit violent ). this exception handling mechanism is called Seh. structured exception handling. if you want to explain what seh is, you can write a blog post here. to put it simply, its principle is similar to the Windows message mechanism. When the program generates a seh exception,
Through the Message notification system, the system calls exception handling routines to handle your exceptions. For this article, you only need to know the following information:

1. It is the exception handling mechanism of the Windows operating system.
2. The following situations fall within the scope of seh processing, such as illegal memory read/write (the first code above), overflow, and division of 0 (the second code above.

In C ++-based project development, we often hope that structured exception can also be captured by try/catch to avoid unexpected results caused by abnormal termination of the program process.

 

Microsoft may have such a demand, so there are some windows-based methods that integrate seh and try/catch exceptions. There are many methods, integration can be implemented at the code level (for example, _ Try, __t T), or you can directly configure it in the IDE tool. for example, the settings in vs2005 are as follows:

In project properties-C/C ++-code generation-enable C ++ exception, select Yes with seh exceptions.

After the preceding code snippet 1 is executed, the output is true:

Catch the exception

 

After reading the above, some people may be excited: as long as you make some simple settings in Vs, you don't have to worry about program crash in the future. Then I will start to pour cold water.

What I want to say is that I wrote this article to analyze the principles and methods, but it does not mean that I advocate this, mainly for the following reasons, I do not advocate integration through configuration in development tools, for the following reasons:

 

1. Chaos. after being set in the IDE environment, seh is mixed with the standard try and catch exceptions. You do not know where to catch the seh exception and where to catch the try catch exception of the standard.

 

2 is not conducive to debugging. Before exception integration, if the program is executed in the debugging status to the memory illegal operation of code segment 1 above, the program will be locked to the wrong location, and tell you the cause of the error. but after integration, it will run to catch, which is not conducive to your discovery of illegal code.

 

3 is not conducive to reading and understanding the code.

 

For the above considerations, we recommend that you use Seh, try, and catch at the code level (for example, you can use _ Try ,__ using t. by default, try/catch and seh are integrated in vc6 (that is to say, using C ++ try/catch can catch windows seh exceptions ), however, this default option is removed in vs2005. What does it mean?

 

In the next article, I plan to write how to combine seh with standard try/catch at the code level.
(However, I have been busy recently and may be late ...)

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.