How will you debug your code? How many ways are there to debug the program? (Submitted by: Shravan)
Debug : means De (remove) bugs (errors). There are 2 things in debugging.
1. Fixing the crashes at run time. Eg: your program crashed at run time, how will fix it?
2. Fixing the bugs at run time. Eg: you have written a function, but it is not working as you desired. Then how will you fix?
This is also called as analyzing the control flow of your program or function.
1. Fixing crashes at run time: This can be done by using LogCat tool in DDMS.
When a program gets crashed, open DDMS in eclipse, open LogCat. It will display all red lines.
Read red lines in the reverse order, find where is your file name in the red errors. double click that like, it will take you to the line
in your program where it has crashed the program.
In the above line, it will also show the exception name why it has crashed.
2. Analyzing the control flow of your program:
For this there are 3 ways.
a. Put toast messages in all the functions to analyze. If it prints toast, then control is going to that function.
b. By using Log.d messages. This is better option compared to toast messages because, toast messages will take lot of time to print.
and it may irritate the programmer. In place of toast if we use Log.d messages, then these messages will be printed in LogCat tool.
There are 5 types of log messages are there.
i. Log.d () -- for debug messages. (These will be in Blue color)
ii. Log.w() -- for warning messages. (Orange color)
iii. Log.e() -- for error messages. (Red color)
iv. Log.v() -- verbose messages. ( Black color)
v. Log.i() -- informative messages. (Green color)
c. By using break points.
Break point - is a point where control will be stopped at the time of program executing.
Once control stops at that point, you can explore what are the values in different variables at that point of time and analyze it.
to go to the next line press F6, to jump to the next break point press F8.
Back To Top