We are doingProgramErrors and exceptions are inevitable during development. How to quickly find the cause of the error, analyze the cause of the error, and find a solution to the problem is a problem that many junior programmers are troubled by. This is also a valuable experience.Next I will briefly introduceVisual StudioAnd some advanced debugging and common errors.
PS: Unless otherwise statedVisual StudioAllDev10That isVisual Studio 2010.
Entry
Assume that you have. NetPlatform programmers andVisual StudioAs a development tool.
Breakpoint: The simplest method is to set a breakpoint. When the program is executed, it is automatically interrupted and enters the debugging status. Set the breakpoint.CodeLine, click on the left side, a red dot is displayed, that is, the breakpoint.
Enable Mode: PressF5, Or the menu bar---Adjust---Start debugging, orIcon
Quick monitoring: You can quickly view the value of a variable or expression, or customize an expression for calculation.
One-step execution
There are three types (F10); One is to execute a row each time, but when a function is called, it will jump to the called function (F11); One is to directly execute the remaining commands in the current function and return the upper-level function (Shift + F11).
There is also a kind of regret medicine, set as the next sentence (Set next statement)That is, the next statement that will be executed (right-click the setting or shortcut key:CTRL + Shift + F10), But note that when debugging is related to data, setting the next sentence may report exceptions. For exampleDatatableWhen adding rows, existing rows cannot be addedDatatableMedium
Monitoring
The debugger may automatically list the values of some related variables, but you may also be concerned about the values of other variables. You can add monitoring for these variables. You can also monitor the value of an expression, suchA + B. However, it is recommended that this expression not modify the value of the variable, such as monitoringA ++Will cause changes during monitoring.AAnd affects the running result of the program.
Debugging skills
Using shortcut keys will greatly improve our debugging efficiency and commonly used debugging shortcuts:
F5Start debugging
F10Execute the next line of code without executing any function call.
F11After the function is called, the code is executed one by one.
Shift + F11The remaining row of the function for executing the current execution point.
Shift + F5Stop the current application in the running program. It can be used in interrupt and run modes.
Drag breakpoint(Thank you for your reminder)
During debugging, we can drag the breakpoint so that the program runs to the desired place. It is usually used to verify whether this code has any impact on the running results of the program. Because we drag the code, the filtered code will not be executed. Compared with the original code, we can see the impact of removing this code.
Conditional interruption
Assume that you writeForLoop, and the number of cycles is relatively large, the following code, now we know inI = 50There will be exceptions, so we cannot press50TimesF5Debug the code. Otherwise, the efficiency will be improved.....
Private Void Conditiondebug ()
{
For (Int I = 0; I <100; I ++)
{
If (I = 50)
{
// Some error code here
Console . Writeline ("I = 50 here" );
}
}
}
We can directly use the functions provided by Vs to modify the value of variable I,OpenI = 0, That isForIn the loop, we setIChange49And press ENTER, Debug again, you will findI = 50;For example
Of course, we can also directly write code in the code to achieve this purpose. The Code is as follows:
Private void conditiondebug ()
{
For (INT I = 0; I <100; I ++)
{
System. Diagnostics. Debug. Assert (I! = 50 );
If (I = 50)
{
// Some error code here
Console. writeline ("I = 50 here ");
}
}
}
UsingAssert(Assertion), when the program is executed, the following prompt box will pop up, clickIngore(Ignore,
You will find thatIAlready50You can check it out if you are interested.Assert.
Immediate window
Immediate windowCalculate the expression value, execute the statement, and print the variable value during debugging. Run the following command:>"), There will be smart prompts, And the names are all self-explanatory.
For example, we want to knowIYou can enter the name> Debug. Print I(It can also be used simply.>? I), Such
ImmediateWindowMore powerful usage: return values of calculation methods (if any)
If this function is available
Int methodvalue (int)
{
If (A = 1)
{
Return 1;
}
Else
{
Return 0;
}
}
We can useImmediateCommand>? Class. Method (ARGs)Call this method, as shown in figure
WherePIs the instance of the current class (becauseMethodvalueIs a class method,Note? And expression)
For some programs with high real-time performance (suchSocket) UseDebug. Write ()Write the error to the log file,. NetYou can setDebugThe information is written to the specified file. Remember that the write in and out information is not necessarily an error message, but also an important information for running your program.,When you find that a module has a problem but cannot determine the location during debugging, you can use this method. If an error occurs only one day, then you must use this method.
Instance
InvolvedWS(WebServices) Debugging
InWinformIn actual development, we often useWSFor data transmission, we obtain the collected data at the front-end.WSThe data is transmitted to the backend. After the backend completes the corresponding business logic processing, the data will last in the database. And we oftenWSWrite related code, such as identity authentication, logging, and prompt information. How can I debug the code.
InvolvedJavascriptDebugging
Many programmers for debuggingJavascriptI am confused because there is no good debugging tool. Some users preferFirebugTo debugJavascriptIs indeed a good choice,FirebugProvides manyJavascriptInformation, is a good debuggingJavascript. Next I will introduce how to useVisual StudioDebuggingJavascriptIn Visual Studio, debugging JS is similar to debugging C #. They all set breakpoints. The difference is that we need to pay attention when viewing element values.
InvolvedAjaxDebugging
NowAjaxIt has become very popular, but it comes with debugging difficulties. Most Junior programmers do not know how to effectively debug the background code from the front-end, and even a lot of imperfectAjaxApplication.
The following is a simple example to describe how to useVisual StudioDebuggingJavascript. Instance is usedAjaxVerify user logon. If the verification succeeds, the system prompts "Logon successful". Otherwise, the system prompts "Logon Failed ".
The following is the main code.JqueryTo achieveAjaxIn the background file.
The correct user name and password areAdminAnd1
The debugging method is as follows: Set a breakpoint at the entrance of the background, and thenJSAnd then pressF5Start debugging. after entering the user name and password, click log on and you will find that the front-end breakpoint is triggered.
PressF5Continue debugging and sometimes jumpJqueryIn the source code, regardless of him, continueF5, You will find the breakpoint that is executed in the background, as shown in figure
The background code debugging is very simple. (PS: Sometimes you can directly go to the background for debugging without setting a breakpoint on the foreground.HtmlFile orAspxSet the breakpoint where an error may occur in the file and debug it step by step)
Some common errors during debugging (will be updated one after another ):
1.When we debug a code sentence, we suddenly jumped out unexpectedly. In fact, this sentence was just executed with an exception and we can use it.Try... CatchCapture exceptions, check the causes of exceptions, and then handle the exceptions accordingly.
2.InAdo. net, We will useDS. Merge ()Memory tables can be merged using the following methods:
A.One table contains two rows of identical data, including the primary key.
B.The two tables have different structures.
C.The type of a field in the two tables does not match, for example, the fieldAgeInATable ChineseStringInBThe table is indeedDecimal
If you have collected some common errors, I hope you can share them.
last, Google is always the best teacher and error solver