Wednesday, May 21, 2014

How to debug java code to fix runtime exceptions

If your java code throws any exceptions during its execution, follow the steps below to find out the cause and fix them.
  1. Look at the stack trace printed in your log file or in the console. Scroll down to see if there is a line starting with "Caused by". Following that line to look for the first line that is about your code. If you could not find a line starting with "Caused by", look from the beginning of the stack trace to find the first line that indicates a place in your code
  2. Look at the place of your code found above to see if there is anything inappropriate, then fix it. You may need to look at the stack trace the second or third lines that are directly related to your code to understand what is going on. If you could not tell what is wrong, you need to debug this section of code.
  3. Debugging java code in NetBeans
    • Setting breakpoint: A breakpoint is a place in the source code that makes the debugger to hang when the execution reaches it. To set a breakpoint, select a line, click the left grey margin area of the line or right click on the left grey margin area of the line, select Breakpoint, then select Toggle Line Breakpoint, the line is highlighted in red with a square red mark on the left margin.
    • Setting a watch: If you would like to monitor the value of a variable, click the Debug button on the top menu and choose New Watch. Enter the variable name that you want to monitor. Or you can right click on the variable name directly and choose New Watch. Click OK. You can monitor the changes of the variable value in the Watches window during debugging.
    • Starting the debugger: Right click on your executable application and choose Debug File. Alternatively, you can select the executable application in the Projects pane, click the Debug button on the top menu and select Debug File. The debugger will execute the program until it reaches a breakpoint, it then hangs. 
    • Using the following keys or corresponding buttons on the top menu to watch the execution of the program.
      • F8 (Step Over): Executes the current line of code. If the current line is a method call, execute the method without stepping into the code of the method.
      • F7 (Step Into): Executes the current line of code. If the current line is a method call, step into the code of the method and stops at the first line of the method .
      • Ctrl+F7 (Step Out): Finishes the execution of the current method and returns to the caller of the method.
      • F5 (Continue): Continue the execution until it reaches the next breakpoint.
      • F4 (Run to Cursor): Continue the execution to the line where the cursor is located
    • While debugging, click the Window button on the top menu, select Debugging, then select Variables, Watches, and any other you would like to monitor. You can monitor the values of the variables in the Variables and Watches windows. Or you can hover the cursor on the variable during line by line execution. You can also highlight an expression by dragging the cursor through it while the left mouse button is pressed, and the hover the cursor over it to see the value of the expression.
  4. Debugging java code in eclipse
    • Setting breakpoint: Select the line of code that you decide to start debugging, go to the left end of the line, double click the small margin area to the left of the line or right click to select Toggle Breakpoint, and the line is marked as a debugging point
    • Starting the debugger: Right-click on the executable application, select Debug as and then select Java Application. If a Confirm Perspective Switch window pops up asking if you want to open the debug perspective, click Yes. If the debugger has already been started, you can click the bug icon on the top menu to start debugging after you having set the debug breakpoint. The execution of the program will hang at the breakpoint.
    • Using the keys described below or corresponding buttons on the top menu to watch the execution of the code
      • F5 (Step into): Executes the current line and goes to the next line. If the current line is a method call, the debugger will step into the code of  the method
      • F6 (Step over): Executes the current line and goes to the next line regardless if the current line is a method call or not. If the current line is a method call, the program executes the method call without the debugger stepping into the code of the method.
      • F7 (Step out): The program finishes the execution of the current method and returns to the caller of this method
      • F8 (Go to next breakpoint): The program will execute until it reaches the next breakpoint.
    • Using the Variables View to analyse the execution. The Variables View displays the names and values of fields and local variables in the current executing stack. To change the display of the view, click the drop down icon at the top-right corner and select the display
  5. Manually debugging
    • If the editor that you use for coding does not have the debugging function. You may select points in your code where you would like to see how the execution progresses, and print to the console a message to indicate that the previous section of code executed successfully and it is entering the next section of code, and if possible attach the values of any pertinent variables using System.out.println("<message>"). If at a place the message stops printing and the program terminates, you know that the problem is located between that place and the place the last message is printed
    • You may also use the Java Debugger (jdb) to debug your code.
         
--------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
--------------------------------------------------------------------------------------------------------------------------------

References

1. Java debugging tutorial - 10 tips on debugging in java with example
2. Java Debugging with Eclipse - Tutorial
3. NetBeans Debugger- Short Tutorial

No comments:

Post a Comment