| title | ms.date | ms.prod | ms.technology | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | translation.priority.ht | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
throw (C# Reference) |
2015-03-02 |
.net |
|
article |
|
|
|
5ac4feef-4b1a-4c61-aeb4-61d549e5dd42 |
22 |
rpetrusha |
ronpet |
|
throw (C# Reference)
Signals the occurrence of an exception during program execution.
Remarks
The syntax of throw is:
throw [e]where e is an instance of a class derived from xref:System.Exception?displayProperty=nameWithType. The following example uses the throw statement to throw an @System.IndexOutOfRangeException if the argument passed to a method named GetNumber does not correspond to a valid index of an internal array.
[!code-cscsrefKeyword#1]
Method callers then use a try-catch or try-catch-finally block to handle the thrown exception. The following example handles the exception thrown by the GetNumber method.
[!code-cscsrefKeyword#2]
Re-throwing an exception
throw can also be used in a catch block to re-throw an exception handled in a catch block. In this case, throw does not take an exception operand. It is most useful when a method passes on an argument from a caller to some other library method, and the library method throws an exception that must be passed on to the caller. For example, the following example re-throws an @System.NullReferenceException that is thrown when attempting to retrieve the first character of an uninitialized string.
[!code-cscsrefKeyword#3]
[!IMPORTANT] You can also use the
throw esyntax in acatchblock to instantiate a new exception that you pass on to the caller. In this case, the stack trace of the original exception, which is available from the @System.Exception.Stacktrace property, is not preserved.
The throw expression
Starting with C# 7, throw can be used as an expression as well as a statement. This allows an exception to be thrown in contexts that were previously unsupported. These include:
-
the conditional operator. The following example uses a
throwexpression to throw an @System.ArgumentException if a method is passed an empty string array. Before C# 7, this logic would need to appear in anif/elsestatement.[!code-cscsrefKeyword#4]
-
the null-coalescing operator. In the following example, a
throwexpression is used with a null-coalescing operator to throw an exception if the string assigned to aNameproperty isnull.[!code-cscsrefKeyword#5]
-
an expression-bodied lambda or method. The following example illustrates an expression-bodied method that throws an @System.InvalidCastException because a conversion to a @System.DateTime value is not supported.
[!code-cscsrefKeyword#6]
C# Language Specification
[!INCLUDECSharplangspec]
See Also
C# Reference
C# Programming Guide
try-catch
The try, catch, and throw Statements in C++
C# Keywords
Exception Handling Statements
How to: Explicitly Throw Exceptions