C # Exception Tip

Source: Internet
Author: User

Exception trapping in C # I believe everyone is familiar with the common use of exception captures are: 1. try{...} catch (Exception ex) {throw ex;} 2. try{...} catch (Exception) {throw;} 3. try{...} catch (Exception ex) {throw new Exception ("Exception description", ex); I believe we are all familiar with these 3 kinds of writing, but I do not know whether we are clear about the subtle differences between the 3 types. Today I would like to share with you the difference between them and in which case it is better to use which method of capture. First of all, let's look at this code:static void Main (string[] args)        {Try            {Test1 ();            }catch (Exception ex)            {Console.WriteLine (ex. Message + Environment.NewLine + ex. StackTrace);            } Console.readkey ();        } static void Test1 ()        {Try            {Test2 ();            }catch (Exception ex)            {//throw ex;//throw;//throw New Exception (ex. Message, ex);            }        } static void Test2 ()        {throw new Exception ("exception thrown here");        }Our Test2 method throws an exception, is called in Test1, and catches the exception. The Test1 method is then called in the upper Main method to capture the exception thrown inside. In Test1 we use 3 different ways of capturing anomalies to see what is the difference.First try{...} catch (Exception ex) {throw ex;} Ways to catch Exceptions:As you can see, the exception stack information captured in the upper method Main method can only capture 33 rows, and our true exception is in 39 rows.look at try{...} catch (Exception) {throw;} Ways to catch Exceptions:As you can see, using this method, the exception stack information is precisely positioned at the location where the exception was generated in 39 rows.finally look at the use of try{...} catch (Exception ex) {throw new Exception ("Exception description", ex);} Different ways to catch exceptions:As you can see, when you catch an exception in this way, the upper method main captures the exception stack information starting with the Test1 method, which is 35 rows, while the exception-generated stack information is contained in the object of the InnerException property. From the above experiments can be seen:the first method of catching an exception loses the real exception stack information. the second can capture the exception stack information completely. The third can also complete the capture of the exception stack information, and can also customize the exception description information. In conclusion, the 3 kinds of capturing methods are more suitable in which case, it is clear that: 1. try{...} catch (Exception ex) {throw ex;} not recommended for use2. try{...} catch (Exception) {throw;} recommended when the exception information is not modified or changed, and is also the default way for code snippets to be generated automatically3. try{...} catch (Exception ex) {throw new Exception ("Exception description", ex);} use when you need to customize the description of the exception information

C # Exception Tip

Related Article

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.