O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Throwing Exceptions

Time for a more structured look at working with exceptions. We’ll start with the act of throwing exceptions to signal an error condition. Not surprisingly, throwing an exception is done using the throw keyword, specifying an instance of an exception type. For example:

void DisplayUserProfile(string user){    if (user == null)        throw new ArgumentNullException("user");    if (user == "")        throw new ArgumentException("exception");    if (!Profiles.Exists(user))        throw new UserNotFoundException(user);    // Do real work here.}

The third exception type used here is a custom exception type, something we explore later. What matters here is the syntax of the throw statement, which is, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required