C # 's throw exception handling statement

Source: Internet
Author: User

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 ();
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.