C # The number of rows where the console output exception resides

Source: Internet
Author: User

For exceptions, we often deal with try-catch statements, a common way to report exceptions in a catch statement block with a MessageBox.Show ("exception") such as a pop-up window. But sometimes, some anomalies occur when we do not want the window to disturb the user, just want to print out the abnormal information to find the cause, analysis and debugging.

The usual method is to print the log to save the exception information to the text, output the exception information to the control that can display the text, and print the exception to the Output window.

The key question involved is how to quickly locate the location of the exception. If you can output the number of rows of the exception is actually a good solution to this problem.

Design an exception as follows: Click Button1 to perform a "divide by 0" exception:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 Ten namespaceWindows_ Console Output One { A      Public Partial classForm1:form -     { -          PublicForm1 () the         { - InitializeComponent (); -         } -  +         Private voidButton1_Click (Objectsender, EventArgs e) -         { +             inti =0; A  at             Try -             {int j = 10/i; -             } -             Catch(Exception ex) -             { in                string str = ex. StackTrace; - Console.WriteLine (str); to             } +             -         } the     } *}

The console Output window prints:

In the Windows_ console output. Form1.button1_click (Object sender, EventArgs e) location c:\Users\happy xia\documents\visual Studio 2013\projects\windows_ Console output \windows_ console output \form1.cs: line number 25

It indicates that the exception appears in the Form1.button1_click method, appears in the file Form1.cs, gives the full path of the file, and finally shows that the number of rows that appear is 25.

In fact, we want to get the most information is:Form1.cs: Line number 25. (Note: If the language selected by Visual Studio is English, the word "number of lines" Here is the corresponding English)

If you consider the compatibility of different locales, it is not the best way to intercept a string with the string "line number" as the tag.

We can consider the last backslash "\" as the Intercept tag. At this point the program is:

1 Private voidButton1_Click (Objectsender, EventArgs e)2         {3             inti =0;4 5             Try6             {7                 intj =Ten/i;8             }9             Catch(Exception ex)Ten             { One                 stringstr =Ex. StackTrace; A                 Console.WriteLine (str. Substring (str. LastIndexOf ("\ \") + 1, str. Length-str. LastIndexOf ("\ \")-1);  -             } -             the}

The console Output window prints:

Form1.cs: Line No. 25

If you want to output the contents of the exception in passing, the code is as follows:

1 Private voidButton1_Click (Objectsender, EventArgs e)2         {3             inti =0;4 5             Try6             {7                 intj =Ten/i;8             }9             Catch(Exception ex)Ten             { One                 stringstr =Ex. StackTrace; A                  Console.WriteLine ("Exception:" + str.) Substring (str. LastIndexOf ("\ \") + 1, str. Length-str. LastIndexOf ("\ \")-1) + "--------" +   ex. Message);   -             } -             the}

The console Output window prints:

Exception: Form1.cs: line number--------try to divide by 0.

C # The number of rows where the console output exception resides

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.