From the user's point of view, when a program encounters an exception, you only need to give a simple prompt. but from the developer's point of view, very detailed information is required, which is useful for tracking bugs. exception-related details can be recorded in log files. in this way, it is easier for you to test and support users. If you encounter any problems, you can send the error log.
The exception handling statement is generally in the following format:
Try
{
// Some code
}
Catch (exception ex)
{
}
We usually get the prompt message ex. message, which briefly describes the exception, but ex also includes other detailed information.
Ex. GetType (). fullname. Detailed Exception name. We know that exception is general, and some more detailed exception names such as system. indexoutofrangeexception
Ex. Source returns the application name
Ex. targetsite returns the name of the function with an exception.
Ex. stacktrace has a lot of information, such as the name of the function with an error, the specific Cs source file, and the line in the source code.
In addition to this information, you may also want to know the version number, operating system of the program running, and hardware information. sometimes some bugs may only appear in an operating system. of course you can use other methods, but it is more convenient to collect the information by writing code.
System. reflection. Assembly. getexecutingassembly (). getname (). version. tostring (); // get the program version number
Obtaining computer software and hardware information
Using system. Management; // Add reference first, find system. mangement, and click OK.
String MSG = string. empty;
Managementobjectsearcher query = new managementobjectsearcher ("select * From win32_operatingsystem ");
Managementobjectcollection querycollection = query. Get ();
Foreach (managementbaseobject managementobject in querycollection)
{
MSG = managementobject ["caption"] + // operating system name, such as Microsoft Windows 7 Professional
Managementobject ["version"] + // system version, for example, 6.1.7601
Managementobject ["manufacturer"] + // operating system manufacturer, such as Microsoft Corporation
Managementobject ["csname"] + // computer name.
Managementobject ["windowsdirectory"]; // system installation directory, such as C: \ WINDOWS
}
Query = new managementobjectsearcher ("select * From win32_computersystem ");
Querycollection = query. Get ();
Foreach (managementobject in querycollection)
{
MSG + = managementobject ["manufacturer"] + // computer vendor, such as Dell Inc.
Managementobject ["model"] + // model, such
Managementobject ["systemtype"] + // CPU type, such as x64-based PC, indicating a 64-bit CPU
Managementobject ["totalphysicalmemory"]. tostring () + // memory size
Managementobject ["Domain"] + // Domain Name
Managementobject ["username"]; // user name when the computer is started
;
}
You can use managementobjectsearcher to view a lot of information related to computer software and hardware, such as hard disks, boards, and NICs.