Send a Patent ~ ~. Topleveleh of net higher order exception handling

Source: Internet
Author: User

We know. NET application runs on the. NET Framework virtual machine, we have try...catch to catch the errors that occur at runtime, and we have a global event that can be subscribed to for WinForm and ASP. See also: http://www.cnblogs.com/eaglet/archive/2009/02/17/1392191.html, but not through the. NET Framework, but directly through p/ Instead of invoking the operating system functionality through the. Net Framework's BCL, invoke invokes the operating system's write-C + + library, and the method described above will lose its effect. For example, I have a method:

[DllImport ("kernel32"privatestaticextern  voidrefbyte src, Int32 len);

Then we drag a button and write an event:

Private void button1_click (object  sender, EventArgs e) {    byte0;     ref 1 ); }

The program crashes directly and is not captured by any of the above methods. Don't believe you can try.

So what? The bell must also be fastened to the person. Do you call the operating system API directly, does the operating system not provide an exception capture mechanism? Some. Now let's introduce a few terms.

Terminology :
SEH: Structured exception handling
VEH: vectorization exception handling
Topleveleh: Top-level exception handling

I believe that students who have written C or C + + programs may not be unfamiliar. Now we describe the operating system exception handling sequence process when an exception is invoked with P/invoke:

1. Submit to the debugger (provided that the process must be debugged )
2. Executive Veh
3. Execute SEH
4. Topleveleh(will not be executed when the process is debugged)
5. To the debugger (the above exception handling is said to be unable to handle, again to the debugger)
6. call the exception port notification Csrss.exe

let's discuss in detail what the various steps are doing deliberately:

1.  The first time to the debugger
  If the program that has the exception is being debugged, the exception is first given to the debugger for processing (via DebugPort). After the debugger gets the exception, it needs to decide whether to handle the exception.

2. Executive Veh
 
Here do not talk about the concept of veh, interested in going to Google a bit.
If it is not debugged, or if the debugger returns dbg_exception_not_handled, the presence of Veh is checked. If there are veh, give them the exception.
Veh is a list of links that can exist in multiple veh. Each veh is called sequentially.
A veh can return a single value: Exception_continue_search, exception_continue_execution. The return exception_execute_handler is invalid, equivalent to Exception_continue_search.
When a veh returns to Exception_continue_search, the exception is given to the next veh processing.
If the return exception_continue_execution is considered to have been processed, the exit exception handler continues execution at the exception command.
From the execution order,Veh is executed before SEH and does not depend on a thread, and any exception in this process can be veh processed , so it is useful in some cases.

So how do you add a veh? This is not the focus of our introduction, please refer to: http://msdn.microsoft.com/en-us/library/ms681420%28VS.85%29.aspx

3. Execute SEH
 
SEH is an exception-handling mechanism based on line stacks. When all veh do not handle the exception, the exception causes SEH to handle it, so it can only handle exceptions to its own thread .

4. Topleveleh

Top-level exception handling, this is the focus of our introduction today, this is actually the use of SEH implementation. In top-level SEH, you can register a top-level exception handler . Although he is based on SEH implementations, it can handle exceptions thrown by all threads .
When SEH does not handle the exception, the top-level SEH checks to see if the topmost exception handling is registered and, if registered, performs top-level exception handling.
Note: If the process is being debugged, top-level exception handling is ignored and will not be executed .
The top-level exception handler function can also return three values:exception_continue_search, Exception_execute_handler, Exception_continue_execution, remember First, It will be useful .   

What's the use of this? The application will inevitably crash, what if it crashes? Think of QQ, each crash will pop up friendly hints, it is how to do it? By the way, this is the Topleveleh.

In this regard, a Java development students have actually applied for a patent:

Publication Number    CN101794243 a Release type    application Patent application number     201010126856 Open Date    August 4, 2010 Application Date    March 18, 2010 Priority Day     101794243  a method for reinforcing Java applications using structured exception handling of the operating system, Java provides methods for invoking local programs in Java programs, which are often present in the form of a dynamic library. Once no catch errors occur in the dynamic library, the entire Java Virtual machine crashes and there is no remedy. By utilizing the structured exception handling mechanism of the operating system, the Java virtual machine can be guaranteed to perform the corresponding remedial actions after the crash, such as making friendly notifications, restarting the Java virtual machine, etc. 

Let's look at the other Java JNI (which is the method that calls the local link library in Java, which corresponds to the. NET P/invoke). And then the patent is still, OK, this patent is too simple, I send to. NET students good.

The patent is quoted by Huawei :

Publication Number    WO2013071766 A1 Release Type    application Patent application number    PCT/cn2012/078524 Open Day    May 23, 2013 application Date    July 12, 2012 priority Day    November 14, 2011 Bulletin No.    cn103107905a, EP2712119A1, US20140115587 applicant    Huawei Technology Co., Ltd. Abstract: The invention discloses an exception handling method, a device and a client, belonging to the online application field. This method includes: the virtual Management Server receives the exception notification sent by the virtual machine interacting with the first client, the exception notification carries at least the user identification and the application identity, the virtual Management Server according to the stored and the user identification and application identity corresponding exception processing method, The application data of the virtual machine data or the application corresponding to the application identity is saved and the virtual machine resources are released. The device includes: receiving module and exception handling module. The invention enables different applications and different users to customize different exception handling methods according to their requirements, and also makes the client not only can save the time when the anomaly occurs or the user's usage state at the moment when the anomaly occurs, but also improves the capacity and efficiency of the online application system. 

plainly is to catch the exception after sending a message, in fact, the so-called patent is such a simple thing.

About 5 and 6 we'll just skip over and talk about how we're going to make it.

First define a delegate:

 Public Delegate Int32 CallBack (reflong  a); CallBack Mycall;

Here's how to register a top-level exception handler:

[DllImport ("kernel32"privatestatic  extern Int32 SetUnhandledExceptionFilter (CallBack CB);

When the system is initialized, call:

New CallBack (Myexceptionfilter); SetUnhandledExceptionFilter (Mycall);
The Myexceptionfilter signature is as follows:
Int32 Myexceptionfilter (reflong a)

It returns an int value corresponding to the above Topleveleh:

Exception_execute_handler = = 1 means I have handled the exception and can gracefully end the
exception_continue_search = = 0 means I don't handle it, the others come, so Windows calls the default handler to display an error box and ends
exception_continue_execution e==-1 indicates that the error has been fixed, please proceed from the point where the exception occurred.

in the actual method, you can record the program's dump file (What is the dump file for?). I'll write another one later, send a message, then gracefully end or restart, to make an excellent customer experience, isn't it great?! Next we deal with the beginning of the exception, it is actually very simple, it must be captured, do not believe you can try.

The code is very simple, I will not upload, the intention of the classmate a try to come out.

Think well written, you can order a praise ~

Reference Link: http://bbs.pediy.com/showthread.php?t=173853

Send a Patent ~ ~. Topleveleh of net higher order exception handling

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.