Exceptions (C# vs Java)

Exception handling in C# is very similar to that of Java.

Whenever something goes critically wrong during the execution of a program, the .NET Framework common language runtime (CLR) creates an Exception object detailing the error. In the .NET Framework, Exception is the base class for all the exception classes. There are two categories of exceptions that derive from the Exception class, SystemException and ApplicationException . All types in the System namespace derive from SystemException while user-defined exceptions should derive from ApplicationException to differentiate between run-time and application errors. Some common System exceptions include:

As in Java, when you have code that is liable to cause an exception, you place that code within a try block. One or more catch blocks immediately after provide the error handling, and you can also use a finally block for any code that you want to execute, whether an exception is thrown or not. For more information, see try-catch (C# Reference), and try-catch-finally (C# Reference).

When using multiple catch blocks, the exceptions caught must be placed in order of increasing generality as only the first catch block that matches the thrown exception will be executed. The C# compiler will enforce this while the Java compiler will not.

Also, C# does not require an argument for a catch block as Java does; in the absence of an argument, the catch block applies to any Exception class.

For example, while reading from a file, you might encounter a FileNotFoundException or an IOException, and you would want to place the more specific FileNotFoundException handler first, as the following code shows:

try
{
    // code to open and read a file
}
catch (System.IO.FileNotFoundException e)
{
    // handle the file not found exception first
}
catch (System.IO.IOException e)
{
    // handle any other IO exceptions second
}
catch
{
    // a catch block without a parameter 
    // handle all other exceptions last
}
finally
{
    // this is executed whether or not an exception occurs 
    // use to release any external resources
}

You can create your own exception classes by deriving from Exception. For example, the following code creates an InvalidDepartmentException class that you might throw if, say, the department given for a new Employee is invalid. The class constructor for your user-defined exception calls the base class constructor using the base keyword, sending an appropriate message:

public class InvalidDepartmentException : System.Exception
{
    public InvalidDepartmentException(string department) : base("Invalid Department: " + department)
    {
    }
}

You could then throw an exception with code like the following:

class Employee
{
    private string department;

    public Employee(string department)
    {
        if (department == "Sales" || department == "Marketing")
        {
            this.department = department;
        }
        else
        {
            throw new InvalidDepartmentException(department);
        }
    }
}

C# does not support checked exceptions. In Java, these are declared using the throws keyword, to specify that a method can throw a particular type of exception that must be handled by the calling code.

See Also

Concepts

C# Programming Guide

Reference

Exceptions and Exception Handling (C# Programming Guide)

Other Resources

The C# Programming Language for Java Developers