. Net exception system-exception Recognition

Source: Internet
Author: User

Exceptions are a type that is often encountered during programming. experience tells us that exception handling is a meticulous task, and the amount of code is often more complex than normal logic code.

An exception occurs when the program is running, such as timeout for connecting to the remote service to obtain data. An exception is different from an error. The former occurs when the function is not completed and exits, the latter means that the function has been executed and Exits normally. The same is that the function does not get the expected results.

 

The. NET Framework does not need to use special methods in the C language to support exception handling. Instead, it uses try, catch, and finally to implement exception handling at the language level. For example:

Capture exceptions 1 try {
2 // put Code requiring graceful recovery and/or cleanup operations here...
3}
4 catch (invalidoperationexception ){
5 // put code that recovers from an invalidoperationexception here...
6}
7 catch (ioexception ){
8 // put code that recovers from an ioexception here...
9}
10 catch {
11 // put code that recovers from any kind of exception other than those abve here...
12 // when catching any exception, you usually re-Throw the exception.
13 // I explain re-throwing later in this chapter.
14 throw;
15}
16 finally {
17 // put code that cleans up any operations started within the try block here...
18 // the code in here always executes, regardless of whether an exception is thrown.
19}

 

. The exception handling mechanism in the. NET Framework adopts the event-like bubbling method. If a function encounters an exception, it is first intercepted by the CLR and then matched with the catch code block based on the exception type, if the matching fails, the CLR calls environment. failfast ends the current process. If the matching succeeds, the catch code block is executed and the finally statement block is executed.

 

In C #, all Exception types are inherited from system. exception, which means that the catch code must be written sequentially from the child type to the parent type according to the type inheritance relationship. Otherwise, the child type exception will be executed to its parent type catch statement. Catch is used to restore or clean up an exception. This part of code should be as simple as possible and will not cause exceptions. Otherwise, exceptions in catch will also be obtained by CLR, an unhandled exception may be thrown out, causing the clr to call Environment. failfast ends the current process. Similarly, finally.

 

System. Exception includes the following attributes:

Message: used to describe the exception information

Source: used to describe the Assembly name

Stacktrace: used to display the runtime call tree

Targetsite: used to display methods for exceptions

Innerexception: used to display internal exceptions

 

Another keyword about the exception in C # Is throw, which is used to actively throw an exception to CLR. It is related to the stacktrace and targetsite of the exception, as shown in the following program.

Throw exception 1 static void main ()
2 {
3 try
4 {
5 throwexpreset ();
6}
7 catch (exception ex)
8 {
9 console. writeline (ex. stacktrace );
10 console. writeline (ex. targetsite );
11}
12
13 try
14 {
15 throwexpwithoutreset ();
16}
17 catch (exception ex)
18 {
19 console. writeline (ex. stacktrace );
20 console. writeline (ex. targetsite );
21}
22
23 console. Readline ();
24}
25
26 Private Static void throwexpreset ()
27 {
28 try
29 {
30 int I = 1;
31 Int J = 0;
32 int K = somemethod (I, j );
33}
34 catch (exception ex)
35 {
36 throw ex;
37}
38}
39
40 Private Static void throwexpwithoutreset ()
41 {
42 try
43 {
44 int I = 1;
45 Int J = 0;
46 int K = somemethod (I, j );
47}
48 catch (exception ex)
49 {
50 throw;
51}
52}
53
54 private static int somemethod (int I, Int J)
55 {
56 return I/J;
57}
58

 

The difference between the above program is that one method only calls throw and the other calls throw exception. In the debug environment, throwexpwithoutreset can output the method name somemethod where an exception occurs, while throwexpreset cannot output the exception method, throw notifies CLR to change the exception start point when an exception occurs, which affects stacktrace and targetsite. A single throw does not notify CLR to change the exception start point, does not change the information of the original exception. However, in the release environment, neither of the two methods can enter the method name that generates an exception, this is because the JIT compiler of C # will compile some methods Inline for optimization purposes in the release environment. Because the method name is absent, it cannot be obtained from the exception information. To obtain the "real" information of an exception, you need to add a property to somemethod to force the compiler not to compile this method using inline, as shown below:

1 [methodimpl (methodimploptions. noinlining)] // forbit the JIT to inline Method
2 Private Static int somemethod (int I, Int J)
3 {
4 return I/J;
5}

 

For unhandled exceptions in the system, methods and call trees that generate exceptions are often used to restore the environment in which exceptions are generated to handle these exceptions. Therefore, when exceptions occur, they cannot be processed, throw should be used. Unless a new exception needs to be thrown, if the granularity of the exception information is not fine enough, you should consider adding the method attribute to prevent the compiler from being inline.

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.