The throw statement is used to emit a signal that an anomalous condition (exception) occurs during program execution. Throw statements are often used in conjunction with Try-catch or try-finally statements. You can use the throw statement to explicitly throw an exception, where a custom exception is thrown. Creating user-defined exceptions, a good encoding method is to "Exception" as the end of the user-defined exception class name.
Use of the example throw statement
This example derives a new exception class useremployeeexception with exception, which defines 3 constructors, each using a different parameter, and then throwing a custom exception. The program code is as follows.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
Namespace Clsuserexecption
{
Class program
{
public class useremployeeexception:exception< br> {
private string errorinfo=string. Empty;
Public useremployeeexception ()
{
}
Public Useremployeeexc Eption (String message): Base (Message)
{
ErrorInfo = message;
Public useremployeeexception (String message, Exception
Inner): Base (Message,inner)
{
ErrorInfo = message;
inner = null;
}
}
public static void Main ()
{
Try
{
throw new Useremployeeexception ("Error Info of Use");
}
catch (useremployeeexception ey)
{
Console.WriteLine ("Output is:");
Console.WriteLine (EY. Message,ey. innerexception);
Console.read ();
}
}
}
}