Microsoft & .NET.NETHandling Exceptions in .NET

Handling Exceptions in .NET

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

A developer’s worst enemy is an exception. Trust me; I know. They can sneak up on you and wreak havoc for an entire day. Therefore, knowing how to handle exceptions is crucial for any developer.

So, where do we start?

Well, we all know the three main culprits in the world of exceptions:

  • Compile time exceptions
  • Runtime exceptions
  • Logic exceptions

For this, with little or not much experience, these seem not too bad. I mean, there are only three types of exceptions, what can really go wrong? Everything. Very quickly.

What Is an Exception?

When something goes wrong within your program, it is because of an exception. A simple example may be a textbox expecting an integer value but receives a string value instead. What should happen in this case? There are two options:

  • Error prevention
  • Handle the exception gracefully

If neither of these are in place, your program will break. It’s as simple as that.

Error Prevention

I am not going to speak too much on this topic, but I still feel it is quite valid here. If you plan properly, very few exceptions will creep in. But, you cannot plan for every possible scenario all the time. Most of the problems that might occur can be handled with some logic built in to your application. This logic could be an if statement checking the validity of data before trying to submit it. This logic could also include ordinary try and catch blocks that try to do a few operations and catch any exception in the event of not being able to complete the try block.

By doing this, you will cut back on the number of exceptions drastically. But, again, you cannot plan for everything. Sometimes, things still go wrong. This is where handling exceptions comes in.

Handling Exceptions

As the name implies: Handling exceptions is a way of dealing with exceptions that have not been catered to or planned for.

So, what are my options?

  • Make use of proper debugging tools
  • Error handling
  • Change in logic

These three above are intertwined and often work together to find and fix an exception.

Debugging Tools Available in Visual Studio 2019

Debugging tools allow you to pause a running program and investigate the values of current objects in memory. With the help of debugging windows, you quickly can identify which values are wrong. By knowing where and why a value has a wrong attribute, you can quickly identify the code and fix it.

Visual Studio 2019 has improved on its debugging capabilities, and I will list and highlight a few here.

Search Bar on Debugging Windows

The Watch, Autos, and Locals windows include a new search feature that enables you to find your variables and their properties quickly. With this new search feature, you can highlight and navigate to specified values, which are contained within the name, value, and type columns of the watch window.

Searching and Highlighting

When entering text in the search bar, the highlighting of matches currently expanded on the screen will occur. This gives you a faster alternative to performing a large-scale search.

Search Navigation

To commence searching in any of the Watch windows, you need to enter the query and press Enter, or, even easier, press the left (find next or F3) and right (find previous or Shift+F3) arrows.

Search Depth

The debugging watch windows provide a Search Depth drop down to find matches nested deep into your objects, giving us the power to choose how thorough the search should be.

Debugging Applications in Visual Studio 2019

In Visual Studio 2019, when you debug your application, it means that you are running the application with the Visual Studio debugger attached. The debugger provides many ways and tools to see what the application’s code is doing while it runs. This includes the following:

  • Stepping through the code to look at the values stored in objects
  • Watches on variables can be set to determine when values change
  • Examine the execution path of the application’s code

Breakpoints

Setting a breakpoint indicates to Visual Studio where it should suspend the application’s running code so that the variables’ values can be inspected, the behaviour of memory can be determined, and whether a certain piece of code can be run.

To set a breakpoint, follow these steps:

  1. Click the left margin (or use the keyboard shortcut: F9 / CTRL+F9). A maroon dot will appear. This is the breakpoint.
  2. Start the program by clicking on the Start button.

Navigating Code During the Debug Mode

We can navigate running code with the use of the following stepping commands:

  • Step into (F11): The debugger steps through code statements one at a time.
  • Step Over (F10): The step over command steps over the execution of functions. This means that it will execute the function without pauses and only pause on the next statement after the function.
  • Step out (Shift + F11): The step out command continues to run the code and pauses the execution when the current function returns.
  • Step into specific: The step into specific command steps into a specific field or property.
  • Run to cursor: The run to cursor starts debugging and sets a temporary breakpoint on the current line of code where the cursor is.

Conclusion

Knowing how to work with exceptions properly can help you plan your projects better. Knowing which tools are available for dealing with exceptions is also crucial in any environment. I have highlighted a few very important ones, but to learn more, you may want to consider reading up on them a bit more in my book: Visual Studio 2019 In Depth.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories