.NET Exceptions – System.Data.ObjectNotFoundException

Making our way through our detailed .NET Exception Handling series, today we’ll tackle the lovely System.Data.ObjectNotFoundException. The System.Data.ObjectNotFoundException is typically used and thrown when dealing with ADO.NET (or other data layer components) and an expected object cannot be found.

Throughout this article we’ll explore the System.Data.ObjectNotFoundException in a bit more detail, starting with a brief look at where it sits in the overall .NET exception hierarchy. We’ll also look at some functional C# code examples illustrating how System.Data.ObjectNotFoundExceptions might be used in your own applications, so let’s get to it!

The Technical Rundown

Full Code Sample

Below is the full code sample we’ll be using in this article. It can be copied and pasted if you’d like to play with the code yourself and see how everything works.

When Should You Use It?

As you may be aware, the .NET Framework includes literally hundreds of built-in Exception types, each intended for a particular scenario or purpose. To that end, many built-in exceptions are part of a specific namespace, so even if the actual name of an exception class doesn’t give much indication about its intended purpose, the namespace to which it belongs can help clue us in.

I bring this up because our little friend, the System.Data.ObjectNotFoundException, is an example of just such an exception. The phrase ObjectNotFound is extremely generic, and the official documentation description is just as vague: “The exception that is thrown when an object is not present.” With these limited clues as to its intended purpose, we’re left to our own devices as developers to decide when it’s most appropriate to use the System.Data.ObjectNotFoundException.

Enter the actual namespace to which the System.Data.ObjectNotFoundException belongs. While ObjectNotFound could obviously apply to any sort of code, System.Data indicates it should be applied to database-specific activities, so that’s where we’ll be using it in our example code. Specifically, our goal is to throw a System.Data.ObjectNotFoundException when one of our attempted SQL queries fails to provide any resulting data.

Before we get into the code, it’s worth noting that we’re using a remote Microsoft Azure SQL Server, which has a copy of the AdventureWorks sample database installed for testing purposes. We’ll be connecting to this server with a basic ADO.NET connection string, along with our credentials that we’ve setup in our project’s Properties > Settings panel in Visual Studio. These settings are then automatically copied in the App.config file, which we can access programmatically when necessary:

We’ve also created a ExecuteQuery(string query) method, which handles all the database connection and query execution logic for us:

We use a SqlConnectionStringBuilder instance to create a connection string using our aforementioned project settings. Once connected, we then perform a rudimentary SqlCommand execution by passing the query parameter as the CommandText property. After executing the command we critically check if any rows are present in the data result. If not, we throw a new System.Data.ObjectNotFoundException and include the problematic query in the error message. Otherwise, we collect the row data and output it to the log.

Again, this is by no means a complete method to actually handle all (let alone most) SQL queries, but it serves the purpose of illustrating how we might use System.Data.ObjectNotFoundExceptions in data-related code. To test this method out we are passing two queries, both of which query customers in the database based on their respective last names:

The first query, which retrieves all records with the LastName of "Miller", works just fine and outputs a handful of customer records:

Now, let’s try the same query, but using the LastName of "Bates", which doesn’t exist for any customer records in the database:

All in all, quite boring if you ask me, but our code did what we asked. Most of the time we’ll get some data back from a query and output said data. However, when a query doesn’t return any results, we throw a System.Data.ObjectNotFoundException. Obviously it’s not typically ideal to use exceptions as means of controlling application flow, but in certain situations I can imagine a use for throwing a System.Data.ObjectNotFoundException when a database query fails to find expected records for one reason or another.

To get the most out of your own applications and to fully manage any and all .NET Exceptions, check out the Airbrake .NET Bug Handler, offering real-time alerts and instantaneous insight into what went wrong with your .NET code, along with built-in support for a variety of popular development integrations including: JIRA, GitHub, Bitbucket, and much more.