Fish and bear's paw -- Mix C ++ exceptions and structural exceptions in VC ++

Source: Internet
Author: User

I haven't updated my blog for a long time. Today is a little bit of free time. I want to write something for those who often follow my blog. Today's topic is about exception handling.

Learn from meC ++AndVC ++EvenWindowsFor a long period of time in programming, exceptions have no concept and are not often used. Even when I seeJavaOfProgramAlmost allCodeI think they are a little abnormal when they are wrapped up with exceptions.

In an accidental opportunity, I did not know why I became the master program of an online game server (in fact, my dream is to write a superb client) and write a service program, the requirement is high stability, high performance, high flexibility, and high scalability. I am not very worried about performance, or even I am not worried about scalability. I will use flexibility.LuaBut stability is a great challenge for me. Is it true that you are not a sage? Therefore, many inexplicable problems are always hidden in the Code. This is a service program. Any problems may cause serious consequences. You can imagine tens of thousands or even hundreds of thousands of people because of your bad service program, while simultaneously hitting the mouse and keyboard, I can only silently pray that they do not know me, otherwise, I would not dare to go to the streets. Speaking of this, we can see how difficult it is to be a programmer!

What I can do in front of these problems is to handle the exception. I started to use it. C ++ Exception Handling, But I was frustrated and found that some exceptions could not be intercepted, and the program was still stubbornly exited, depressed, and super depressed, so I thought about giving up this damn C ++ Exception Handling, for use Windows The system's structured exception processing, but worse things happen, the syntax of the structural exception is too weird, and there is no Throw To use a function Raiseexception , At the same time C ++ Some exceptions cannot be intercepted. Ah, it's really a fish and a bear's paw! What should I do? With a try attitude, I wrote C ++ Some structured exception handling code is also used. When the result is compiled, the compiler tells me that I cannot C ++ To use a structured exception, you need to open /ESCs Switch. C/C ++ It is found in the Code Generation option under the option, and the option name is enabled C ++ Exception. There is one in the drop-down list. /ESCs So I used it and started to compile it. Oh, great! The Code has been compiled. I feel very good, but wait, it seems that the problem is still not solved, and the two types of exception handling are still difficult in the code. What should I do? I don't know what to do.

UsuallyVC ++When programmers do not know what to do, the best way is (note not to access the Internet)-but to openMsdn, I found/EhThe help of the options is useless information. I am used to making some connections. One of them is called_ Set_se_translatorFunction makes my eyes shine, with years of readingMsdnI believe there is a shocking secret behind this experience. Microsoft's help is like this. There is a lot of useful information hidden behind a bunch of useless instructions, just like the Shaolin monk who hid jiuyang Zhenjing In the Diamond Sutra.

After clicking the past, the first sentence made me very excited. It said:

Handles Win32 exceptions (C structured exceptions) as C ++ typed exceptions.

Direct translation means processing.Win32Exception (CStyle structuring exception) ImageC ++Type exception.C ++Exception style handlingCStyle exception. This is my real purpose. in the true sense, we can mix two types of exception processing and useC ++The style is simple and elegant, and the structural exception is powerful and efficient. I was wondering if some programmers at Microsoft encountered such problems as I did, so they invented this great method?

To use this function, you can set a pointer to handle structural exceptions and implement this function on your own.

_ Set_se_translatorThe function prototype is as follows:

_ Se_translator_function _ set_se_translator (

_ Se_translator_function setransfunction

);

The prototype of the structured exception handling function is as follows:

Typedef void (* _ se_translator_function) (unsigned int, struct _ prediction_pointers *);

Next, I will directlyMsdnThe example code in is here for your reference to prevent some netizens from using it.MsdnI am not familiar with this function and cannot find it for half a day.

// Crt_settrans.cpp

// Compile with:/EHA

# Include <stdio. h>

# Include <windows. h>

# Include <eh. h>

 

Void sefunc ();

Void trans_func (unsigned int, exception_pointers *);

 

Class se_exception

{

PRIVATE:

Unsigned int neuron;

Public:

Se_exception (){}

Se_exception (unsigned int N): neuron (n ){}

~ Se_exception (){}

Unsigned int getsenumber () {return neuron ;}

};

Int main (void)

{

Try

{

_ Set_se_translator (trans_func );

Sefunc ();

}

Catch (se_exception E)

{

Printf ("caught a _ Try exception with se_exception./N ");

}

}

Void sefunc ()

{

_ Try

{

Int X, Y = 0;

X = 5/y;

}

_ Finally

{

Printf ("in finally/N ");

}

}

Void trans_func (unsigned int U, prediction_pointers * pexp)

{

Printf ("in trans_func./N ");

Throw se_exception ();

}

 

Finally, in my own project, I also used a similar design style. I defined a more powerful exception class than the exception class in this example, and finally achievedC ++The perfect combination of exceptions and structural exceptions. Of course, my service programs have become exceptionally robust, all exceptions have been intercepted, and the program is very stable. So far, there is a good solution to the stability problem of service programs. Of course, I cannot rely entirely on a sound Exception Handling Mechanism. The more errors inherent in the program, the better. This is what I need to do after intercepting exceptions.

I hope this article will truly help those netizens who are fighting for server-side program development like me!

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.