Summary: in this tutorial, you’ll learn how to use the C# throw
keyword to raise an exception in your application.
Introduction to the C# throw keyword
The throw
keyword allows you to raise an exception. Here’s the syntax of how to use the throw
keyword:
throw exception;
Code language: C# (cs)
In this syntax, the exception
is an object that represents the exception that you want to raise. You can use any built-in exception class that is derived from the System.Exception
class. Also, you can raise a custom exception, which will cover in the next tutorial.
Typically, you use the throw
keyword with an if
statement to throw an exception once a certain condition is met:
if (condition)
{
throw exception;
}
Code language: C# (cs)
Using the C# throw keyword to raise an exception example
The following program demonstrates how to use the throw
keyword to throw an ArgumentOutOfRangeException
exception:
using static System.Console;
public class Circle
{
public double Radius
{
get; set;
}
public Circle(double radius)
{
if (radius <= 0)
{
throw new ArgumentOutOfRangeException(
nameof(radius),
"The radius should be positive"
);
}
Radius = radius;
}
public double GetArea() => Math.PI * Radius * Radius;
}
class Program
{
public static void Main(string[] args)
{
WriteLine("Enter a radius:");
var input = ReadLine();
if (input != null)
{
var radius = double.Parse(input);
try
{
var circle = new Circle(radius);
WriteLine($"The area is {circle.GetArea():F2}");
}
catch (ArgumentOutOfRangeException ex)
{
WriteLine(ex.Message);
}
}
}
}
Code language: C# (cs)
How it works.
First, define a class called Circle
that represents a circle with a given radius.
In the Circle
class, we define a constructor that takes a double
value representing the radius of the circle. If the value of the radius is less than or equal to zero, we throw an ArgumentOutOfRangeException
with a custom error message using the throw
keyword.
In the Circle
class, we also define a method GetArea()
that returns the area of the circle using the formula πr².
Second, prompt the user to enter a radius in the Main
method of the program.
The Main()
method:
- Reads the input from the console
- Parses it to a
double
- Creates an instance of the
Circle
class with the entered radius value - Calls the
GetArea()
method to calculate the area of the circle.
If the entered radius is not positive, the Main
method catches the ArgumentOutOfRangeException
exception raised by the constructor and displays the error message on the console.
The throw Expression
Starting from C# 7.0, you can use the throw
keyword as an expression. This allows you to throw an exception inline, without having to define a separate variable. The syntax of the throw
expression is as follows:
throw expression;
Code language: JavaScript (javascript)
In this syntax, the expression
evaluates to an object derived from the System.Exception
class. For example, you can change the Circle
constructor in the previous example to the following:
public Circle(double radius)
{
Radius = radius > 0
? radius
: throw new ArgumentOutOfRangeException(
nameof(radius),
"The radius should be positive"
);
}
Code language: C# (cs)
In this example, if the radius
is greater than zero, we assign it to the Radius
property. Otherwise, we throw the ArgumentOutOfRangeException
exception. This has the same effect as the previous example but is more concise.
Summary
- Use the C#
throw
keyword to raise an exception explicitly.