nedcomp hosting homepage

Producten en diensten
Dedicated servers
Datacenter informatie
Partners, resellers
Helpdesk informatie
Technische docs, tools
Support homepage
ASP componenten
Praktische ASP, ASP.NET
Visual route server
Whois (domein gegevens)
Software documentatie
Whitepapers
Zoeken
Nedcomp / algemeen

Zoeken
 

Copyright © Nedcomp Hosting
Telefoon nr :   +31 184 670111
Fax nummer :   +31 184 631384
E-mailadres :   info@nedcomp.nl
 

C# Programmer's Reference  

throw

The throw statement is used to signal the occurrence of an anomalous situation (exception) during the program execution. It takes the following form:

throw [expression];

where:

expression
The exception object. This is omitted when rethrowing the current exception object in a catch clause.

Remarks

The thrown exception is an object whose class is derived from System.Exception, for example:

class MyException : System.Exception {}
throw new MyException();

Usually the throw statement is used with try-catch or try-finally statements. When an exception is thrown, the program looks for the catch statement that handles this exception.

You can also rethrow a caught exception using the throw statement. For more information and examples, see try-catch, 8.10 The try statement and Throwing Exceptions.

Example

This example demonstrates how to throw an exception using the throw statement.

// throw example
using System;
public class ThrowTest 
{
   public static void Main() 
   {
      string s = null;

      if (s == null) 
      {
         throw(new ArgumentNullException());
      }

      Console.Write("The string s is null"); // not executed
   }
}

Output

The following exception occurs:

System.ArgumentNullException

Example

See the try-catch, try-finally, and try-catch-finally examples.

See Also

<_pluslang_the_try.2c_.catch.2c_.and_throw_statements>Compare to C++ | C# Keywords | Exception Handling Statements | Throwing Exceptions | C. Grammar