Tuesday, August 27, 2024

10 Examples of Conditional Breakpoint Debugging in Java and Eclipse

Eclipse is a great tool for Java developers, it allows you to write code, execute them and debug them but many developers doesn't use Eclipse to their full potential. I focus a lot of debugging skills when I take interviews and when I ask about how they debug their Java application, many developer mention use of line breakpoint and only few mention about conditional breakpoint. To be honest, if you are debugging a real-world Java application which has lots of users, clients or data then line breakpoint will not be very helpful and it will take ages to find the most of the data related issues. 

For example, somewhere in your code you getting a NullPointerException and ArrayIndexOutOfBoundsException and you are not sure for which client, user or value this error came. 

From the exception stack trace, you will know where exactly the code is throwing exception but when you to try to debug using line breakpoint, you might need to try thousands time before you face an erroneous value. 

Sometime using an appropriate Exception breakpoint is useful but if you remote debugging and Exception breakpoint is not showing you the values you are interested in, conditional breakpoint is the best way to go forward. 

It will allow you to stop the code execution when given condition is met. 

This will help you to zero down the problem and quickly lead you to solution rather than spending hours to find the root cause using line breakpoint. 

In my opinion, line breakpoint is only useful to understand the flow but for any data related error, conditional breakpoint is the best feature of Eclipse IDE.

Since I have used conditional breakpoint extensively for years and I still love it, I am going to share some of the tricks which I use along with conditional breakpoint. 

Though, some of you might know these debugging tips, I believe it will help many beginners to learn some debugging tricks which will save lot of time while troubleshooting Java errors in future.


10 Coding Breakpoint examples in Eclipse for Java Developers

Here are my favorite examples of putting conditional break point while debugging a Java applications:


1. Inserting print statement without deploying new JAR

You won't appreciate this trick unless you have tried to remote debug a Java application and tired of trying to find the bug even with conditional debugging. 

When all my theories about bad data goes wrong I simply insert some print statement and take break. 

When I come back I have some useful data in log file until application is crashed and many a times it leads me to root cause. 

The beauty of this trick is that you don't need to deploy a new JAR file. All you need to do is just add some print statement in conditional debugging area and return false. 

The conditional breakpoint will not stop the execution because you mostly choose "suspend when condition is true" but it will execute the code and print value of key variable in the log file, which you can analyze later.


Here is how to insert print statement in the code without deploying new JAR:


This tip is even more helpful while debugging issues lying on core libraries which doesn't provide context e.g. a bug in a method which copies part of array into another object.



2. Stopping a particular Thread

In multi-threading application same code is executed by multiple threads. These threads has their own local variable and context hence some issues only occur when a particular thread execute the code. 

If your stack trace print error in a particular thread, you should debug your program conditionally and stop execution for that thread. 

This will not only save time but also makes debugging easier. In order to stop execution of Java application on a particular thread, you can use the Thread.currentThread() method.

For example, you are getting ArrayIndexOutOfBoundsException in main() thread than you can add following code in conditional debugging text area to stop execution on that line when code is executed by main thread.



3. Hit Count

The Hit Count feature allow you to suspend execution after a line has been executed certain number of times. 

In order to enable this feature just elect Hit count option and enter number of iterations you want to skip. 

To be honest, I don't have many scenarios where someone need to use HitCount but I have used it in past to skip the first call. 

If you have used it to debug any particular scenario, please share your experience.




4. Stopping method execution when null is passed

From here on, the conditional breakpoint example is just about common conditions i.e. I will teach you how to suspend execution of code when a method argument is null or a field is null, or a String is empty etc. Just pay attention what you need to insert in conditional break point tab. 

The execution will stop when data passed to method or state of objects matches with your condition




5. Stopping method execution in case of empty String

Just stop the thread when a String is empty using either length = 0 or isEmpty() = true



6. Stopping method execution in case of a particular value

This is similar to previous example, you can stop at a method when input value matches with your expected value. 



7. Stopping execution when argument length is greater than certain value



8. Debugging when list is empty

Similar to String empty example, you can also stop the execution when a List or Collection is empty. 



9. debugging when array is empty

This is same as previous example, instead of List, you can also do the same thing with array in Java. 



10. Debugging when list contains a particular object.

You can also stop execution of your program when a list contains a particular value for example a List of topics contains an interesting topic you want to test. 






Points to remember

Here are important points you can remember while using conditional breakpoint debugging in Eclipse or IntelliJ IDEA


1. Condition you set must return Boolean value. Make sure will JAVA allow me to set this condition in IF statement . You can use complex conditions by using && and/or ||.


2. If there is possibility to having null value for object , then better to use null checking to avoid errors , eg. list.get(i) != null && list.get(i).toString.equals("Java").


3. You can use auto complete (CTRL + Space) in the condition text box

4. Note that breakpoint condition is same as normal Java code. So take care of scope of variables used in condition and possibilities of Compile/Run time exceptions and yes, always check your condition is returning Boolean.



That's all about 10 examples of conditional breakpoint debugging in Java and Eclipse. You can even use these tips in other popular Java IDEs like NetBeans and IntelliJ IDEA because they also support conditional breakpoint and even though I have not tested e.g. inserting print statement or stopping a particular thread, I expect that they will work there too. 

Remember, debugging will eventually tell you the root cause but point is to find it quickly. 

If you are not efficient with debugging and doesn't know your tools then you can spend hours and hours with line breakpoint and exception breakpoint to find the root cause. 

For both experienced and beginners Java programmers is to learn conditional breakpoint debugging and embrace it in your day to day debugging.

    No comments:

    Post a Comment