DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Using Conditional Breakpoints to Filter Exceptions During Debugging

Did you know that with C# 6 you can now filter exceptions? Well, you can! Read on to find out how to make use of this handy new feature.

Dror Helper user avatar by
Dror Helper
·
Nov. 10, 16 · Tutorial
Like (2)
Save
Tweet
Share
5.92K Views

Join the DZone community and get the full member experience.

Join For Free

With every new version the C# language has grown and improved. The last version so a.k.a C# 6 has brought some of my favorite features. And with C# 7 just around the corner, I know there's more to come.

One of the new useful features added to C# is the ability to filter exceptions. Consider the following completely (and poorly) invented class:

public class MyClass 
public class MyClass
{
    public void MyMethod(int count)
    {
        throw new MyException(1000 + count);
    }
}

If I need to exit only when an exception with a specific error code is thrown I can write the following code:

class Program
{
    static void Main(string[] args)
    {
        var myClass = new MyClass();

        for (int i = 0; i < 10; i++)
        {

            try
            {
                myClass.MyMethod(i);
            }
            catch (MyException ex) when (ex.ErrorCode == 1003)
            {
                Console.WriteLine($"Got the right exception in iteration: {i}");
                break;
            }
            catch (MyException ex)
            {
                Console.WriteLine($"Other exception: {ex.ErrorCode}");
            }
        }
    }
}

And each exception with value which is not 1003 will be swallowed. There are limitless scenarios in which this feature can be useful — for example, have a look at Jimmy Bogard's post on how to avoid exceptions on the weekends.

But what happens when I need the same functionality but just for a specific debug session or if I'm working on a project which does not use C# 6 (yet?). For those cases, I can use the magic of conditional breakpoints.

Filtering With Conditional Breakpoints

There are two ways to set a conditional breakpoint - the old way and the OzCode way.

If you never used them, you should; it's a time-saving tool which will save you valuable debug time.

In order to use Conditional breakpoints just create a regular breakpoint at the exception's constructor and then add condition to that breakpoint - simple as that. With OzCode it's even easier:

  1. Create a "regular" breakpoint at the exception's constructor
  2. Run until breakpoint
  3. Open the quick watch window
  4. Choose the property you care about
  5. Update the condition
  6. Press "ok" and run

Conditional_breakpoints

Once you fix the bug you can delete the conditional breakpoint and continue working – or find and fix another bug.

Conclusion

Conditional breakpoints have been part of your favorite IDE of choice forever and yet not all developers use them as often as they should. Catching just the right exception is one scenario where they help find and fix bugs and save us time in which we can develop new features for our customers or just grab another cup of coffee.

Filter (software)

Published at DZone with permission of Dror Helper, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Utilizing Database Hooks Like a Pro in Node.js
  • Reconciling Java and DevOps with JeKa
  • Data Stream Using Apache Kafka and Camel Application
  • Best Practices for Setting up Monitoring Operations for Your AI Team

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: