C# Exception Overview

by Sam Allen - Updated January 7, 2010
Warning

You are alerted that the .NET runtime has raised an exception, meaning something has occurred that normally shouldn't. Learn ways to deal with abnormal conditions. Here is a collection of tips and solutions to exceptions in the C# programming language. An exception's name is the first big clue.

What is NullReferenceException?

It means you have a null object you are trying to use, which is not possible in the C# language. This exception can be hard to debug. This website has some examples, and then I point out when this occurs in programs. This is raised when you call an instance method on a null object. Find more about this exception on this site.

(See NullReferenceException and Null Parameter.)

NullReferenceException details. Many times, methods return null when they cannot return the requested object. Check function calls with debugger breakpoints. Set debug printouts to track behavior. Obviously, you should check for null to avoid the exception. However, there are times when you shouldn't, such as when null is not a value that is possible given normal conditions.

What is IndexOutOfRangeException?

It means you are accessing a memory location that's not allocated. One fix is to use bounds-checking on a getter or setter. However, if the error condition is unlikely to happen, it is more efficient and graceful to handle it through try and catch blocks. In the following example, you should change the max value of the loop.

/// <summary>
/// If you use the square brackets and an index that isn't present
/// in the array or collection, you will go out of range.
/// </summary>
public void ExampleC()
{
    //
    // This will raise an index out of range exception.
    //
    string example = "cat";
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(example[i]);
    }
}

Why should I use foreach loops?

To avoid exceptions like the ones above. With a foreach loop, another function takes control of the iteration process. You don't need to know the collection's indexes. Here are some solutions to the IndexOfOutRange exception.

/// <summary>
/// Some ways to solve an out of range exception.
/// </summary>
public void ExampleD()
{
    string example = "cat";
    //
    // use try/catch
    //
    try
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(example[i]);
        }
    }
    catch (IndexOutOfRangeException)
    {
        return;
    }

    //
    // or move to enumerators and foreach
    //
    foreach (char c in example)
    {
        Console.WriteLine(c);
    }
}

How do I fix an InvalidCastException?

Change your code to use generic objects that specify the type. Alternatively, use the special 'as' or 'is' operators. This often occurs when you are dealing with object types. When you try to use that object as something it is not, it will throw this exception.

/// <summary>
/// This nasty example shows one way you can get into trouble with
/// casting and get an InvalidCastException.
/// </summary>
public void ExampleE()
{
    string box = "cat";
    object general = box as object;

    int a = (int)general;
}

/// <summary>
/// This code shows you how to avoid the exception and only cast to
/// int when you are sure the object is an int.
/// </summary>
public void ExampleF()
{
    string box = "cat";
    object general = box as object;

    int a = 0;
    if (general is int)
    {
        a = (int)general;
    }
}

Exception is thrown in first method. The compiler doesn't catch the error in the first method at compilation, so it throws on runtime. The box variable is a string, but we attempt to use it as an int. That's an invalid cast if there ever were one.

As or is are the best. As or is are special operators that do efficient and safe casting. If in doubt, use these operators and test for success. As returns null if it doesn't succeed. It is often best to use as.

Design flaw? Usually, treating different objects as vanilla objects—meaning you don't have information about their type—is a very bad design. It may be time to switch to generics or build special types.

What is FileNotFoundException?

It means the file is not on the disk. This site has some examples of code blocks that may be causing this exception. When you assume a file is present and it isn't, you will get this exception. Simply add logic to File.Exists for this case, or if the situation is really abnormal, catch this exception.

(See FileNotFoundException Tips.)

What is DirectoryNotFoundException?

An exception that's thrown when a directory isn't found. The DirectoryNotFoundException is thrown when a method, most commonly Directory.GetDirectories(), receives an invalid path. Programs like Paint.NET use a method that ensures the directory exists. You can find more about paths on this site.

What does KeyNotFoundException mean?

This occurs because the key doesn't exist in the Dictionary. You can fix with the TryGetValue method or simply more error checking. Add some debugging points in your application and try to find out if you assign to a key that doesn't exist in your Dictionary. You must use the Add method to put a value in the collection with a key.

(See KeyNotFoundException Fix.)

I have a StackOverflowException. Why?

You could be using recursion or some part of your algorithm may be out of control. If you are experiencing a stack overflow exception, there is a very serious problem with your code. Your method may call itself endlessly, or two methods may call each other. The problem is probably in your algorithm. Please see the exceptions category on this site for a listing of articles that includes the StackOverflowException article.

(See Stack Overflow [StackOverflowException].)

What are some database exceptions?

Here's a list of them. These exceptions are thrown by ADO.NET provider objects. They involve some of my most frustrating moments as a developer. When you get one of these following exceptions, you may be using invalid SQL. The SQL engine may or may not provide more details.

Using SqlException. You may need to fix your SQL statement. This can be triggered by trying to update a nonexistent table in your database, or something even worse.

(See SqlClient Tutorial.)

Using SqlCeException. This is for the SQLCE embedded database engine. I had a horribly frustrating time with this as I couldn't figure out how to get more detailed error messages.

(See SQLCE 3.5 Database Tutorial.)

Using OdbcException. This is the open database connection exception, and it too is caused sometimes by bad SQL.

(See OdbcConnection Example.)

Summary

Here we saw an overview of several different kinds of exceptions that can be thrown by the CLR in the .NET Framework. There are places where exceptions are not required, and debugging statements are better. Our first task is to gain knowledge about the exceptions thrown by the runtime, and our second task is simply to catch them or avoid them in the first place.

(Do not copy this page.)

Dot Net Perls | Search
Exceptions | Collection Was Modified Exception | File.Delete Exceptions | FileNotFoundException Tips | KeyNotFoundException Fix | NullReferenceException and Null Parameter
C# | Decrement Int | HTML Brackets | Parameter Optimization Tip | SaveFileDialog Tutorial
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen