[C #] custom exceptions

Source: Internet
Author: User

First, we create our own exception class customexception, which inherits from the applicationexception class (this is a general exception thrown when a non-fatal application error occurs, it inherits from a more general exception class) and uses it as the base class for any custom exceptions defined by the application. With this base class, we can write a common catch code block to capture any custom exception types defined by the application.

Custom exception types can play a very important role in the middle. We can capture a common exception, identify its relationship with the application, and then throw it as an application-specific exception to handle it as appropriate.

A simple scenario is built here. The northwind database is used to add a customer record to it. If the ID is repeated, an exception message is thrown.

 

(1) define our basic exception classes

Public class customexception: applicationexception

...{

Public customexception ()

...{

}

 

Public customexception (string message, exception inner): Base (message, inner)

...{

}

}

Define two methods to handle exceptions. Use the base keyword to let the customexception method inherit from the basic error exception class applicationexception and provide two parameters, one exception message parameter and one exception error class.

(2) then, let's determine an error handling class that handles different custom error types. Different Application exception types use different exception handling classes.

Public class duplicatecustomeridexception: customexception

...{
Public duplicatecustomeridexception ()

...{

}
Public duplicatecustomeridexception (string message, exception inner): Base (message, inner)

...{

}
}

When an exception of the corresponding type occurs, you can perform a series of processing when an error occurs in the custom exception handling process, and then throw the exception information, for example, record the error log, or fault tolerance.

(3) When adding a customer record, we use our custom exception

Private void addcustomerrecord ()

...{

Sqlconnection Cn = new sqlconnection (dbconn );

CN. open ();

 

Try

...{

Sqlcommand COM = new sqlcommand ("insert into MERs (customerid, companyName, contactname) values ('" + ftxt_customerid.text + "', '" + ftxt_companyname.text + "', '"+ ftxt_contactname.text +"') ", CN );

Com. executenonquery ();

}

Catch (sqlexception ex)

...{

If (ex. Number = 2627)

...{

Throw new duplicatecustomeridexception ("repeated mermerid", ex );

}

Else

...{

MessageBox. Show ("successful", "prompt message", messageboxbuttons. OK, messageboxicon. Information, messageboxdefaultbutton. button1, messageboxoptions. defaulttoptoponly );

}

}

Finally

...{

CN. Close ();

}

}

Ex. Number = 2627 is the exception number thrown when the record with duplicate primary keys cannot be added due to repeated primary keys in the data table. If the exception type is correct, how can we throw a custom exception handling class.

(4) Finally, when we call this method, write the code to catch the exception of this custom type.

Private void fbtn_submit_click (Object sender, system. eventargs E)

...{

Try

...{

Addcustomerrecord ();

}

Catch (duplicatecustomeridexception ex)

...{

MessageBox. Show (ex. Message );

}

}

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.